Java Threads परिचय
In this page:
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?
// 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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Main Thread Overview
हर Java program execution एक single main thread से शुरू होता है जिसे JVM आपके main() method के चलने से पहले ही अपने आप बना देता है। आप किसी भी समय Thread.currentThread() का उपयोग करके इस thread को inspect और interact कर सकते हैं, जो logging या concurrency समस्याओं को debug करने के लिए सुविधाजनक है।
उदाहरण: Main Thread Overview
// 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());
}
}
Login to try C/C++/Java/PHP code in the editor
Multi-threaded Execution
कई threads spawn करके, कोई program सब कुछ एक पर क्रमवार चलाने के बजाय अलग-अलग tasks को concurrently execute कर सकता है और कई CPU cores का फायदा उठा सकता है। यह खासतौर पर उस CPU-bound काम के लिए कीमती है जिसे स्वतंत्र हिस्सों में बांटा जा सकता हो, या I/O-bound काम के लिए जो अन्यथा इंतज़ार करते हुए एक core को खाली छोड़ देता।
उदाहरण: Multi-threaded Execution
// 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();
}
}
Login to try C/C++/Java/PHP code in the editor
t.start()के बजायt.run()call करना, जो सिर्फ current thread में code चलाता है, एक नया thread बनाने के बजाय।- एक ही
Threadपर दो बारstart()call करना, जोIllegalThreadStateExceptionफेंकता है। - यह मान लेना कि threads एक fixed order में print होते हैं, जबकि scheduler तय करता है, इसलिए runs के बीच output अलग हो सकता है जब तक आप
join()या synchronization का उपयोग न करें।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: