1.AbortPolicy:抛出异常
throws a {@code RejectedExecutionException}.
private static void testAbortPolicy() throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1),
r -> {
Thread thread = new Thread(r);
return thread;
}, new ThreadPoolExecutor.AbortPolicy());
for (int i = 0; i < 3; i++) {
executor.execute(() -> {
try {
TimeUnit.SECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
TimeUnit.SECONDS.sleep(1);
executor.execute(() -> {
System.out.println("Can this task execute???");
});
}
![](https://img.haomeiwen.com/i12022128/89e89cb10fbbd491.png)
2.DiscardPolicy: 拒绝任务
silently discards the rejected task.
private static void testDiscardPolicy() throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1),
r -> {
Thread thread = new Thread(r);
return thread;
}, new ThreadPoolExecutor.DiscardPolicy());
for (int i = 0; i < 3; i++) {
executor.execute(() -> {
try {
TimeUnit.SECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
TimeUnit.SECONDS.sleep(1);
executor.execute(() -> {
System.out.println("Can this task execute???");
});
}
![](https://img.haomeiwen.com/i12022128/7547b5449f11f2c3.png)
3.CallerRunsPolicy:直接使用调用线程执行任务
runs the rejected task directly in the calling thread of the {@code execute} method,unless the executor has been shut down, in which case the task is discarded.
private static void testCallerRunsPolicy() throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1),
r -> {
Thread thread = new Thread(r);
return thread;
}, new ThreadPoolExecutor.CallerRunsPolicy());
for (int i = 0; i < 3; i++) {
executor.execute(() -> {
try {
TimeUnit.SECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
TimeUnit.SECONDS.sleep(1);
executor.execute(() -> {
System.out.println("Can this task execute??? Yes, is execute in thread " + Thread.currentThread().getName());
});
}
![](https://img.haomeiwen.com/i12022128/d3688c79b1cabe17.png)
4.DiscardOldestPolicy:抛弃队列中的未执行的任务,尝试重新执行
discards the oldest unhandled request and then retries {@code execute},unless the executor is shut down, in which case the task is discarded.
private static void testDiscardOldestPolicy() throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1),
r -> {
Thread thread = new Thread(r);
return thread;
}, new ThreadPoolExecutor.DiscardOldestPolicy());
for (int i = 0; i < 3; i++) {
executor.execute(() -> {
try {
TimeUnit.SECONDS.sleep(5);
System.out.println("I am from lambda.");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
TimeUnit.SECONDS.sleep(1);
executor.execute(() -> {
System.out.println("Can this task execute??? Yes, is execute in thread " + Thread.currentThread().getName());
});
}
![](https://img.haomeiwen.com/i12022128/399c44415bc1cad9.png)
网友评论