← Back to Advanced Java Course | Chapter 4: Modern Java Features | Lesson 9 of 12

Java के Virtual Threads

Virtual threads, जो Project Loom के ज़रिए Java 21 में finalized हुए, lightweight JVM-managed threads हैं जो किसी program को blocking-style tasks की भारी संख्या को सस्ते में और concurrently चलाने देते हैं।
Syntax
java
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?

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

Creating a Virtual Thread

Virtual threads नए Thread.ofVirtual() builder या shortcut Thread.startVirtualThread का उपयोग करके बनाई जाती हैं, दोनों calling code के नज़रिए से एक normal Thread की तरह व्यवहार करती हैं लेकिन कहीं कम overhead लेकर।

उदाहरण: Creating a Virtual Thread

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

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

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

Virtual Threads with ExecutorService

Executors.newVirtualThreadPerTaskExecutor() इसमें submit किए गए हर task के लिए एक नई virtual thread बनाता है, एक fixed-size pool को reuse करने के बजाय, जो अच्छी तरह काम करता है क्योंकि virtual threads प्रति task एक बनाने के लिए काफी सस्ती हैं।

उदाहरण: Virtual Threads with ExecutorService

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

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

java
// 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");
	}
}
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. यह उम्मीद करना कि program virtual threads का इंतज़ार करेगा, जबकि वे daemon threads हैं, इसलिए main पहले exit हो सकता है जब तक आप join() call न करें।
  2. virtual threads को एक fixed-size pool में रखना, जबकि वे सस्ती हैं और प्रति task एक बनाने के लिए हैं।
  3. CPU-heavy काम के लिए virtual threads का उपयोग करना, जबकि वे blocking I/O में मदद करती हैं और उन tasks के लिए कोई speed-up नहीं देतीं जो CPU को व्यस्त रखते हैं।

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.