-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathMyThreadFactory.java
More file actions
39 lines (32 loc) · 1.11 KB
/
Copy pathMyThreadFactory.java
File metadata and controls
39 lines (32 loc) · 1.11 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
package ch3.s2;
import java.util.concurrent.*;
public class MyThreadFactory {
public static class MyTask implements Runnable {
public void run() {
System.out.println(System.currentTimeMillis() + "thread id:" + Thread.currentThread().getId());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
MyTask task = new MyTask();
ExecutorService es = new ThreadPoolExecutor(5, 5,
0L, TimeUnit.MILLISECONDS,
new SynchronousQueue<Runnable>(),
new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
System.out.println("create " + t);
return t;
}
});
for (int i = 0; i < 5; i++) {
es.submit(task);
}
Thread.sleep(2000);
}
}