-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualThreadsDemo.java
More file actions
101 lines (89 loc) · 4.09 KB
/
Copy pathVirtualThreadsDemo.java
File metadata and controls
101 lines (89 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package java20.virtualthreads;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.stream.IntStream;
/**
* Java 20 Virtual Threads (Second Preview)
* Demonstrates lightweight virtual threads
*
* NOTE: Requires --enable-preview flag for compilation and execution
* Compile: javac --enable-preview --release 20 VirtualThreadsDemo.java
* Run: java --enable-preview VirtualThreadsDemo
*/
public class VirtualThreadsDemo {
public static void main(String[] args) {
System.out.println("=== Java 20 Virtual Threads (Second Preview) ===\n");
// 1. Create virtual thread directly
System.out.println("1. Virtual Thread Creation");
System.out.println("--------------------------");
Thread virtualThread = Thread.ofVirtual()
.name("worker-", 0)
.start(() -> {
System.out.println("Running on virtual thread: " + Thread.currentThread().getName());
System.out.println("Is virtual: " + Thread.currentThread().isVirtual());
});
try {
virtualThread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// 2. Using virtual thread executor
System.out.println("\n2. Virtual Thread Executor");
System.out.println("--------------------------");
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 5; i++) {
final int taskId = i;
executor.submit(() -> {
System.out.println("Task " + taskId + " on virtual thread: " +
Thread.currentThread().getName());
try {
Thread.sleep(100); // Simulate I/O operation
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
}
}
// 3. Builder pattern
System.out.println("\n3. Builder Pattern");
System.out.println("------------------");
Thread.Builder builder = Thread.ofVirtual().name("task-", 0);
Thread vt1 = builder.start(() -> System.out.println("Task 1"));
Thread vt2 = builder.start(() -> System.out.println("Task 2"));
try {
vt1.join();
vt2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// 4. High-throughput example
System.out.println("\n4. High-Throughput Example");
System.out.println("-------------------------");
System.out.println("Creating 1000 virtual threads...");
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
IntStream.range(0, 1000).forEach(i -> {
executor.submit(() -> {
try {
Thread.sleep(10); // Simulate I/O
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
});
}
System.out.println("Completed 1000 tasks with virtual threads!");
// 5. Comparison with platform threads
System.out.println("\n5. Virtual Threads vs Platform Threads");
System.out.println("--------------------------------------");
System.out.println("Virtual Threads:");
System.out.println(" - Managed by JVM");
System.out.println(" - Millions can be created");
System.out.println(" - Perfect for I/O-bound operations");
System.out.println(" - Blocking doesn't block OS thread");
System.out.println("\nPlatform Threads:");
System.out.println(" - Managed by OS");
System.out.println(" - Limited (thousands)");
System.out.println(" - Better for CPU-bound operations");
System.out.println("\nVirtual threads are lightweight and perfect for I/O-bound operations!");
}
}