-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaTest.java
More file actions
48 lines (34 loc) · 1.07 KB
/
JavaTest.java
File metadata and controls
48 lines (34 loc) · 1.07 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
import org.junit.jupiter.api.Test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class JavaTest {
@Test
public void t() {
// 创建虚拟线程
try (ExecutorService taskExecutor = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 10; i++) {
int finalI = i;
taskExecutor.submit(() -> {
TimeUnit.MILLISECONDS.sleep(500L);
System.out.println(finalI);
return finalI;
});
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
void t2() {
// 创建虚拟线程,默认不启动
Thread thread = Thread.ofVirtual().name("test").unstarted(() -> {
System.out.println("test");
});
thread.start();
// 创建虚拟线程
Thread.ofVirtual().name("test2").start(() -> {
System.out.println("test2");
});
}
}