Java Thread का Lifecycle
In this page:
Thread States Overview
एक thread अपने lifecycle के दौरान कई अलग-अलग states से गुज़रता है: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, और TERMINATED। JVM इन state transitions को अपने आप manage करता है जैसे-जैसे एक thread बनता है, schedule होता है, रुकता है, और आखिरकार चलना खत्म करता है।
उदाहरण: Thread States Overview
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {});
System.out.println(t.getState()); // NEW
t.start();
t.join();
System.out.println(t.getState()); // TERMINATED
}
}
Login to try C/C++/Java/PHP code in the editor
New and Runnable States
एक thread बनने के क्षण लेकिन उस पर start() call होने से पहले NEW state में रहता है। start() call करते ही यह तुरंत RUNNABLE state में चला जाता है, जहां यह CPU scheduling के लिए eligible हो जाता है लेकिन ज़रूरी नहीं कि अभी चल रहा हो।
उदाहरण: New and Runnable States
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> System.out.println("running"));
System.out.println(t.getState()); // NEW
t.start();
System.out.println(t.getState()); // RUNNABLE
t.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Timded Waiting State
एक thread TIMED_WAITING state में तब जाता है जब वह खुद को एक explicit, निश्चित अवधि के लिए रोक लेता है, आमतौर पर static Thread.sleep(milliseconds) method call करके। एक indefinite WAITING state के विपरीत, एक timed-waiting thread उस अवधि के खत्म होते ही अपने आप फिर से शुरू हो जाता है, बिना किसी बाहरी signal के भी।
उदाहरण: Timded Waiting State
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
try { Thread.sleep(200); } catch (InterruptedException e) {}
});
t.start();
Thread.sleep(50);
System.out.println(t.getState()); // TIMED_WAITING
t.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Blocked and Waiting States
एक thread specifically BLOCKED state में तब जाता है जब वह किसी resource पर lock पाने का इंतज़ार कर रहा हो जो फिलहाल कोई दूसरा thread रखता है। यह अधिक सामान्य WAITING state में तब जाता है जब यह किसी दूसरे thread के कोई काम करने के इंतज़ार में अनिश्चित काल तक रुका हो, जैसे notify() call करना या किसी join() को पूरा करना।
उदाहरण: Blocked and Waiting States
public class Main {
static final Object lock = new Object();
public static void main(String[] args) throws InterruptedException {
Thread holder = new Thread(() -> {
synchronized (lock) {
try { Thread.sleep(200); } catch (InterruptedException e) {}
}
});
holder.start();
Thread.sleep(50);
Thread blocked = new Thread(() -> {
synchronized (lock) { }
});
blocked.start();
Thread.sleep(50);
System.out.println(blocked.getState()); // BLOCKED
holder.join();
blocked.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Terminated State
एक thread TERMINATED state में तब जाता है जब उसका run() method execute होना खत्म कर देता है, चाहे सामान्य रूप से या किसी uncaught exception फेंककर। एक terminated thread हमेशा के लिए खत्म है और इसे restart नहीं किया जा सकता; उस code को फिर से चलाने के लिए आपको एक बिल्कुल नया Thread object बनाना होगा।
उदाहरण: Terminated State
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> System.out.println("done"));
t.start();
t.join();
System.out.println(t.getState()); // TERMINATED
}
}
Login to try C/C++/Java/PHP code in the editor
- यह मानना कि RUNNABLE का मतलब है कि thread अभी चल रहा है, जबकि इसका मतलब सिर्फ इतना है कि यह चलने के लिए तैयार है या चल रहा है और शायद CPU का इंतज़ार कर रहा हो।
- किसी TERMINATED thread पर फिर से
start()call करना, जोIllegalThreadStateExceptionफेंकता है क्योंकि एक thread को restart नहीं किया जा सकता। - BLOCKED और WAITING को मिला देना: BLOCKED का मतलब है किसी
synchronizedlock का इंतज़ार करना, जबकि WAITINGwait(),join()या similar calls से आता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: