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