forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT28.java
More file actions
51 lines (40 loc) · 1.25 KB
/
T28.java
File metadata and controls
51 lines (40 loc) · 1.25 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
package com.basic;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @program JavaBooks
* @description: 第五种,WorkStealingPool
* @author: mf
* @create: 2020/01/01 15:05
*/
public class T28 {
public static void main(String[] args) throws IOException {
// 默认核数
ExecutorService service = Executors.newWorkStealingPool();
service.execute(new R(1000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
service.execute(new R(2000));
// 由于产生的是精灵线程(守护线程、后台线程),主线程如果不阻塞的话,看不到输出
System.in.read();
service.shutdown();
}
private static class R implements Runnable {
int time;
public R(int time) {
this.time = time;
}
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
}