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

Java Threads परिचय

एक thread किसी program के अंदर काम की एक अलग line है, जैसे एक ही kitchen में एक साथ दो cooks। यह किसी program को कई काम एक साथ करने देता है।
Syntax
java
class ClassName extends Thread {
    public void run() {
        // thread code
    }
}

new ClassName().start();

What is a Thread?

एक thread एक lightweight sub-process है जो आपके application के अंदर concurrently चलता है, उस process के बाकी हर thread के साथ एक ही memory space share करता है। यह shared memory threads को अलग processes की तुलना में बनाना कहीं सस्ता बना देती है, network calls या file I/O जैसे तेज़, non-blocking background काम को संभव बनाते हुए।

उदाहरण: What is a Thread?

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws InterruptedException {
		// Create a new Thread object and store it in t
		Thread t = new Thread(() -> System.out.println("Running in background thread"));
		// Start the thread; it runs in parallel with this code
		t.start();
		// Wait for the thread to finish
		t.join();
	}
}

The Thread Class

आप built-in Thread class को extend करके और उसके run() method को उस code से override करके एक custom thread declare कर सकते हैं जो background में execute होना चाहिए। यह approach सीधा है लेकिन inheritance का आपका एकमात्र मौका बांध देता है, क्योंकि Java classes सिर्फ एक parent को extend कर सकती हैं।

उदाहरण: The Thread Class

java
// Define the class Main
public class Main {
	// Define the class MyThread
	static class MyThread extends Thread {
		public void run() {
			// Print a line to the console
			System.out.println("Custom thread running");
		}
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws InterruptedException {
		// Create a new MyThread object and store it in t
		MyThread t = new MyThread();
		// Start the thread; it runs in parallel with this code
		t.start();
		// Wait for the thread to finish
		t.join();
	}
}

Starting a Thread

किसी thread को असल में शुरू करने के लिए, आप अपना Thread object initialize करते हैं और सीधे run() call करने के बजाय उसका start() method call करते हैं। start() call करने से thread JVM के scheduler के साथ register हो जाता है, जो फिर तय करता है कि run() method को असल में एक अलग call stack पर कब execute करना है।

उदाहरण: Starting a Thread

java
public class Main {
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread(() -> System.out.println("Executed via start()"));
		t.start(); // registers with JVM scheduler
		t.join();
	}
}

Main Thread Overview

हर Java program execution एक single main thread से शुरू होता है जिसे JVM आपके main() method के चलने से पहले ही अपने आप बना देता है। आप किसी भी समय Thread.currentThread() का उपयोग करके इस thread को inspect और interact कर सकते हैं, जो logging या concurrency समस्याओं को debug करने के लिए सुविधाजनक है।

उदाहरण: Main Thread Overview

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Thread current = Thread.currentThread();
		// Print a line to the console
		System.out.println("Running on: " + current.getName());
	}
}

Multi-threaded Execution

कई threads spawn करके, कोई program सब कुछ एक पर क्रमवार चलाने के बजाय अलग-अलग tasks को concurrently execute कर सकता है और कई CPU cores का फायदा उठा सकता है। यह खासतौर पर उस CPU-bound काम के लिए कीमती है जिसे स्वतंत्र हिस्सों में बांटा जा सकता हो, या I/O-bound काम के लिए जो अन्यथा इंतज़ार करते हुए एक core को खाली छोड़ देता।

उदाहरण: Multi-threaded Execution

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) throws InterruptedException {
		// Create a new Thread object and store it in t1
		Thread t1 = new Thread(() -> System.out.println("Task 1"));
		// Create a new Thread object and store it in t2
		Thread t2 = new Thread(() -> System.out.println("Task 2"));
		// Start the thread; it runs in parallel with this code
		t1.start();
		// Start the thread; it runs in parallel with this code
		t2.start();
		// Wait for the thread to finish
		t1.join();
		// Wait for the thread to finish
		t2.join();
	}
}
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. t.start() के बजाय t.run() call करना, जो सिर्फ current thread में code चलाता है, एक नया thread बनाने के बजाय।
  2. एक ही Thread पर दो बार start() call करना, जो IllegalThreadStateException फेंकता है।
  3. यह मान लेना कि threads एक fixed order में print होते हैं, जबकि scheduler तय करता है, इसलिए runs के बीच output अलग हो सकता है जब तक आप join() या synchronization का उपयोग न करें।

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.