Is lesson me hum seekhenge:
- Multithreading basics
- join() method kya hota hai
- join() ka use kyun hota hai
- join() ka working
- Real examples
Multithreading ka matlab:
ek program me multiple threads ka parallel execution
Example:
music play + download + UI response same time
Java me thread banane ke 2 tarike:
1. Thread class extend karke
2. Runnable interface implement karke
join() method ka use hota hai:
ek thread ko wait karne ke liye jab tak dusra thread complete na ho jaye
thread.join();class Test extends Thread {
public void run(){
for(int i = 1; i <= 3; i++){
System.out.println("Thread running: " + i);
}
}
public static void main(String[] args) throws Exception {
Test t1 = new Test();
t1.start();
t1.join(); // main thread wait karega
System.out.println("Main thread finished");
}
}t1.start();
System.out.println("Main thread finished");Output unpredictable hoga:
Main thread pehle print ho sakta hai
t1.start();
t1.join();Output:
thread complete hone ke baad hi next line execute hogi
class Test extends Thread {
public void run(){
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) throws Exception {
Test t1 = new Test();
Test t2 = new Test();
t1.start();
t1.join(); // t1 complete
t2.start(); // fir t2 start
}
}t1.join(); // complete hone tak wait
t1.join(1000); // max 1 second wait
t1.join(1000, 500); // nanoseconds bhiProject me:
Task A complete hone ke baad hi Task B start ho
✔ join() thread coordination ke liye use hota hai
✔ InterruptedException handle karna padta hai
✔ execution order control kar sakte hain
| Feature | join() | sleep() |
|---|---|---|
| Purpose | wait for another thread | pause current thread |
| Dependency | dusre thread par | khud par |
- join() method kya hota hai?
- join() aur sleep() me difference?
- kya join() multiple threads me use hota hai?
- join() bina try-catch kyun nahi chal sakta?
Is lesson me humne seekha:
✔ Multithreading basics
✔ join() method
✔ execution control
✔ practical examples
join() Java me thread synchronization aur execution order control ke liye use hota hai.