CONCURRENCY (Java)
๐ง CONCURRENCY (Java) โ MINI STUDY GUIDE
๐น 1. What is Concurrency?
Definition (simple lang):
Concurrency means multiple tasks running at the same time.
๐ Think of it like this:
Nagla-laptop ka ๐ป habang naka-play music ๐ง
Or nagdo-download habang nagba-browse
๐ก This part is important kasi ito โyung foundation ng topic.
Key Ideas:
A multithreaded program has multiple parts running at once
Helps improve efficiency (di sayang oras habang naghihintay)
๐ From your handout (page 1):
Concurrency allows tasks waiting for resources to give way to other tasks.
๐น 2. Thread vs Process
Concept | Meaning | Easy Example |
|---|---|---|
Thread | Smallest unit of execution | One task inside an app |
Process | Group of threads | Whole app (e.g., Chrome) |
๐ฅ Real-Life Analogy:
Process = Restaurant ๐ฝ
Threads = Waiters serving tables ๐จโ๐ณ
๐ Multiple waiters (threads) = faster service
๐ Important:
Threads in the same process share memory
Meaning: mabilis communication between them
๐น 3. How Threads Work (Scheduler + Context Switch)
๐ง Thread Scheduler
OS decides which thread runs next
๐ Context Switch
Saving current thread โ switching to another โ then returning later
๐ Parang:
You pause Netflix โ do homework โ balik ulit sa Netflix
๐ From page 1:
Context switching stores and restores thread state.
๐น 4. Main Thread in Java
Every Java program starts with main thread
public static void main(String[] args)๐ This is the starting point of everything
๐ก You can create more threads to run tasks simultaneously
๐น 5. Ways to Create Threads (SUPER IMPORTANT ๐ฅ)
โ Method 1: Extend Thread Class
class MyThread extends Thread {
public void run() {
System.out.println("Running thread");
}
}๐ Then:
MyThread t = new MyThread();
t.start();โ Method 2: Implement Runnable Interface
class MyThread implements Runnable {
public void run() {
System.out.println("Running thread");
}
}๐ Then:
Thread t = new Thread(new MyThread());
t.start();โ๏ธ Comparison
Extend Thread | Implement Runnable |
|---|---|
Simpler | More flexible |
Canโt extend another class | Can extend other classes |
Less reusable | More reusable |
๐ก In short:
Use Runnable if you want flexibility ๐ฏ
๐น 6. start() vs run() (COMMON CONFUSION โ )
Method | What it does |
|---|---|
| Starts new thread |
| Code executed by thread |
๐ Important:
start()โ creates new threadrun()โ just runs like normal method if called directly
๐ก In short, ganito lang โyan:
๐ start() = real multithreading
๐ run() = normal function lang
๐น 7. Thread Methods (Core Tools)
From page 2:
Method | Purpose |
|---|---|
| Start thread |
| Main logic |
| Name thread |
| Get thread name |
| Set priority |
| Get priority |
| Check if running |
| Wait for thread |
| Pause thread |
๐น 8. Thread Priority
Range: 1 (low) โ 10 (high)
Default: 5
๐ From page 2:
MIN_PRIORITY = 1MAX_PRIORITY = 10NORM_PRIORITY = 5
โ Important:
Higher priority โ guaranteed faster execution
It just has higher chance to run
๐น 9. Useful Thread Methods Explained
๐ค sleep()
Pauses thread
Thread.sleep(3000); // 3 seconds๐ Example:
Waiting before next action (like loading screen)
โณ join()
Wait for another thread to finish
๐ Example:
Wait for download โ then open file
๐งฌ isAlive()
Checks if thread is still running
t.isAlive(); // true or false๐ฅ Example Flow (from pages 3โ4)
Thread starts
Sleeps (pause)
Ends
Then check if alive โ false
๐น 10. Thread.currentThread()
๐ Gets the currently running thread
Thread.currentThread().getName();๐ก Useful for debugging or tracking execution
๐ง QUICK SUMMARY (Memory Mode)
๐ฅ Core Idea:
Concurrency = doing multiple things at the same time
๐งฉ Easy Memory Tricks
1. TPC Rule
Thread = small task
Process = container
Concurrency = multitasking
2. Start vs Run
๐ โStart creates, Run executesโ
3. Thread Creation
๐ โExtend OR Implementโ
Extend Thread
Implement Runnable
4. Thread Methods Shortcut
๐ SRSP GGIJS
Start
Run
Sleep
Priority
Get/Set Name
Get/Set Priority
isAlive
Join
(Okay medyo sabog mnemonic but it works ๐ญ)
๐ง FINAL RECAP (Super Simplified)
๐ Concurrency = sabay-sabay na tasks
๐ Threads = workers
๐ Process = whole system
๐ start() = new thread
๐ run() = actual work
๐ sleep/join/isAlive = control tools