-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualThreadTest.java
More file actions
36 lines (25 loc) · 1.08 KB
/
VirtualThreadTest.java
File metadata and controls
36 lines (25 loc) · 1.08 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
package jep444;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
public class VirtualThreadTest {
/* Thread.Builder, Thread.ofVirtual(), and Thread.ofPlatform() are new APIs to create virtual
and platform threads.
Thread.startVirtualThread(Runnable) is a convenient way to create and then start a virtual thread.
A Thread.Builder can create either a thread or a ThreadFactory, which can then create multiple threads with identical properties.
Thread.isVirtual() tests whether a thread is a virtual thread.
Thread.getAllStackTraces() now returns a map of all platform threads rather than all threads.
*/
@Test
void createVirtualThread() throws Exception {
AtomicInteger integer = new AtomicInteger(0);
Thread vthread = Thread.ofVirtual().name("vthread").unstarted(() -> {
System.out.println("executing vthread");
integer.compareAndExchange(1, 5);
});
integer.set(1);
vthread.start();
Thread.sleep(50L);
assertThat(integer.get()).isEqualTo(5);
}
}