-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathRejectThreadPoolDemo.java
More file actions
35 lines (30 loc) · 1.11 KB
/
Copy pathRejectThreadPoolDemo.java
File metadata and controls
35 lines (30 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
package ch3.s2;
import java.util.concurrent.*;
public class RejectThreadPoolDemo {
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 LinkedBlockingDeque<Runnable>(10),
Executors.defaultThreadFactory(),
new RejectedExecutionHandler() {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println(r.toString() + " is discard");
}
});
for(int i=0;i<Integer.MAX_VALUE;i++){
es.submit(task);
Thread.sleep(10);
}
}
}