修复线程池关闭时,阻塞策略仍然可以将任务添加到线程池

This commit is contained in:
witt 2022-06-17 11:55:32 +08:00
parent 231e0cf1d7
commit 4670dccb67

View File

@ -3,6 +3,7 @@ package cn.hutool.core.thread;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;
/**
* 当任务队列过长时处于阻塞状态直到添加到队列中
@ -14,15 +15,35 @@ import java.util.concurrent.ThreadPoolExecutor;
*/
public class BlockPolicy implements RejectedExecutionHandler {
/**
* 线程池关闭时为避免任务丢失留下处理方法
* 如果需要由调用方来运行可以{@code new BlockPolicy(Runnable::run)}
*/
private final Consumer<Runnable> handlerwhenshutdown;
public BlockPolicy(final Consumer<Runnable> handlerwhenshutdown) {
this.handlerwhenshutdown = handlerwhenshutdown;
}
public BlockPolicy() {
this(null);
}
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
try {
e.getQueue().put(r);
} catch (InterruptedException ex) {
throw new RejectedExecutionException("Task " + r + " rejected from " + e);
// 线程池未关闭时阻塞等待
if(!e.isShutdown()){
try {
e.getQueue().put(r);
} catch (InterruptedException ex) {
throw new RejectedExecutionException("Task " + r + " rejected from " + e);
}
return;
}
// 当设置了关闭时候的处理
if(null != handlerwhenshutdown){
handlerwhenshutdown.accept(r);
}
}
}