Java के Virtual Threads
In this page:
Thread.ofVirtual().start(runnable);
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
executor.submit(task);
}
What are Virtual Threads?
Virtual threads, जो Project Loom के हिस्से के रूप में Java 21 में finalized हुए, पूरी तरह JVM द्वारा manage किए जाने वाले lightweight threads हैं बजाय किसी operating system thread से एक-के-लिए-एक map होने के, जिससे किसी program को उनकी भारी संख्या सस्ते में चलाने की अनुमति मिलती है।
उदाहरण: What are Virtual Threads?
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread vt = Thread.ofVirtual().start(() -> System.out.println("Running on a virtual thread")); // JVM-managed, lightweight
vt.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Creating a Virtual Thread
Virtual threads नए Thread.ofVirtual() builder या shortcut Thread.startVirtualThread का उपयोग करके बनाई जाती हैं, दोनों calling code के नज़रिए से एक normal Thread की तरह व्यवहार करती हैं लेकिन कहीं कम overhead लेकर।
उदाहरण: Creating a Virtual Thread
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t1 = Thread.ofVirtual().start(() -> System.out.println("via ofVirtual()"));
Thread t2 = Thread.startVirtualThread(() -> System.out.println("via startVirtualThread")); // shortcut
t1.join(); t2.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Virtual Threads vs Platform Threads
एक platform thread, जो new Thread() से बनाई जाती है, सीधे एक सीमित operating system thread से map होता है, जबकि एक virtual thread JVM द्वारा carrier threads के एक छोटे pool पर schedule की जाती है, जिससे लाखों virtual threads वहां मौजूद रह सकती हैं जहां सिर्फ हज़ारों platform threads मौजूद हो सकती थीं।
उदाहरण: Virtual Threads vs Platform Threads
// 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 platform
Thread platform = new Thread(() -> System.out.println("Platform thread: " + Thread.currentThread()));
Thread virtual = Thread.ofVirtual().unstarted(() -> System.out.println("Virtual thread: " + Thread.currentThread()));
// Start the thread; it runs in parallel with this code
platform.start(); platform.join();
// Start the thread; it runs in parallel with this code
virtual.start(); virtual.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Virtual Threads with ExecutorService
Executors.newVirtualThreadPerTaskExecutor() इसमें submit किए गए हर task के लिए एक नई virtual thread बनाता है, एक fixed-size pool को reuse करने के बजाय, जो अच्छी तरह काम करता है क्योंकि virtual threads प्रति task एक बनाने के लिए काफी सस्ती हैं।
उदाहरण: Virtual Threads with ExecutorService
// Import java.util.concurrent.* so it can be used by its short name
import java.util.concurrent.*;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws Exception {
// Try code that might throw an exception
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
executor.submit(() -> System.out.println("New virtual thread per task"));
}
}
}
Login to try C/C++/Java/PHP code in the editor
When to Use Virtual Threads
Virtual threads उच्च-throughput, I/O-bound workloads जैसे कई concurrent network requests या database calls को handle करने के लिए आदर्श हैं, क्योंकि किसी virtual thread को block करना सस्ता है, लेकिन वे CPU-bound computation के लिए बहुत कम फायदा देती हैं, जो processor cores से सीमित है न कि thread count से।
उदाहरण: When to Use Virtual Threads
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println("Good fit: many I/O-bound network/database calls");
// Print a line to the console
System.out.println("Poor fit: CPU-bound computation, limited by processor cores");
}
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि program virtual threads का इंतज़ार करेगा, जबकि वे daemon threads हैं, इसलिए
mainपहले exit हो सकता है जब तक आपjoin()call न करें। - virtual threads को एक fixed-size pool में रखना, जबकि वे सस्ती हैं और प्रति task एक बनाने के लिए हैं।
- CPU-heavy काम के लिए virtual threads का उपयोग करना, जबकि वे blocking I/O में मदद करती हैं और उन tasks के लिए कोई speed-up नहीं देतीं जो CPU को व्यस्त रखते हैं।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: