← Back to Advanced Java Course | Chapter 2: Multithreading | Lesson 2 of 8

Java Thread का Lifecycle

एक thread अपने जीवन में कई stages से गुज़रता है, जैसे कोई व्यक्ति जो तैयार हो रहा है, दौड़ रहा है, इंतज़ार कर रहा है, और आखिरकार खत्म। इन stages को जानना यह समझने में मदद करता है कि एक thread क्या कर रहा है।

Thread States Overview

एक thread अपने lifecycle के दौरान कई अलग-अलग states से गुज़रता है: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, और TERMINATED। JVM इन state transitions को अपने आप manage करता है जैसे-जैसे एक thread बनता है, schedule होता है, रुकता है, और आखिरकार चलना खत्म करता है।

उदाहरण: Thread States Overview

java
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
	}
}

New and Runnable States

एक thread बनने के क्षण लेकिन उस पर start() call होने से पहले NEW state में रहता है। start() call करते ही यह तुरंत RUNNABLE state में चला जाता है, जहां यह CPU scheduling के लिए eligible हो जाता है लेकिन ज़रूरी नहीं कि अभी चल रहा हो।

उदाहरण: New and Runnable States

java
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();
	}
}

Timded Waiting State

एक thread TIMED_WAITING state में तब जाता है जब वह खुद को एक explicit, निश्चित अवधि के लिए रोक लेता है, आमतौर पर static Thread.sleep(milliseconds) method call करके। एक indefinite WAITING state के विपरीत, एक timed-waiting thread उस अवधि के खत्म होते ही अपने आप फिर से शुरू हो जाता है, बिना किसी बाहरी signal के भी।

उदाहरण: Timded Waiting State

java
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();
	}
}

Blocked and Waiting States

एक thread specifically BLOCKED state में तब जाता है जब वह किसी resource पर lock पाने का इंतज़ार कर रहा हो जो फिलहाल कोई दूसरा thread रखता है। यह अधिक सामान्य WAITING state में तब जाता है जब यह किसी दूसरे thread के कोई काम करने के इंतज़ार में अनिश्चित काल तक रुका हो, जैसे notify() call करना या किसी join() को पूरा करना।

उदाहरण: Blocked and Waiting States

java
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();
	}
}

Terminated State

एक thread TERMINATED state में तब जाता है जब उसका run() method execute होना खत्म कर देता है, चाहे सामान्य रूप से या किसी uncaught exception फेंककर। एक terminated thread हमेशा के लिए खत्म है और इसे restart नहीं किया जा सकता; उस code को फिर से चलाने के लिए आपको एक बिल्कुल नया Thread object बनाना होगा।

उदाहरण: Terminated State

java
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
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मानना कि RUNNABLE का मतलब है कि thread अभी चल रहा है, जबकि इसका मतलब सिर्फ इतना है कि यह चलने के लिए तैयार है या चल रहा है और शायद CPU का इंतज़ार कर रहा हो।
  2. किसी TERMINATED thread पर फिर से start() call करना, जो IllegalThreadStateException फेंकता है क्योंकि एक thread को restart नहीं किया जा सकता।
  3. BLOCKED और WAITING को मिला देना: BLOCKED का मतलब है किसी synchronized lock का इंतज़ार करना, जबकि WAITING wait(), join() या similar calls से आता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.