这有效
- 它派生自SingleThreadExecutor,但您可以轻松地进行调整
- Java 8 lambdas代码,但易于修复
它将创建一个单线程执行器,可以处理大量任务;并且会等待当前任务结束执行才开始下一个任务
如果发生未捕获的错误或异常,则uncaughtExceptionHandler将捕获它
public final class SingleThreadExecutorWithExceptions {
/**
* 创建一个带有未捕获异常处理程序的单线程执行器。
*
* @param uncaughtExceptionHandler 未捕获异常处理程序
* @return 返回创建的带有未捕获异常处理程序的单线程执行器
*/
public static ExecutorService newSingleThreadExecutorWithExceptions(final Thread.UncaughtExceptionHandler uncaughtExceptionHandler) {
// 自定义线程工厂实现类,用于创建线程并设置未捕获异常处理程序
ThreadFactory factory = (Runnable runnable) -> {
final Thread newThread = new Thread(runnable, "SingleThreadExecutorWithExceptions");
// 设置未捕获异常处理程序
newThread.setUncaughtExceptionHandler( (final Thread caughtThread,final Throwable throwable) -> {
uncaughtExceptionHandler.uncaughtException(caughtThread, throwable);
});
return newThread;
};
// 创建带有任务队列的线程池
return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue(),
factory){
// 重写afterExecute方法,在执行完线程任务后会对异常进行处理
protected void afterExecute(Runnable runnable, Throwable throwable) {
super.afterExecute(runnable, throwable);
if (throwable == null && runnable instanceof Future) {
try {
Future future = (Future) runnable;
if (future.isDone()) {
future.get();
}
} catch (CancellationException ce) {
throwable = ce;
} catch (ExecutionException ee) {
throwable = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (throwable != null) {
uncaughtExceptionHandler.uncaughtException(Thread.currentThread(), throwable);
}
}
});
}
/**
* 继承自ExecutorService的包装类,只暴露ExecutorService实现的方法。
*/
private static class DelegatedExecutorService extends AbstractExecutorService {
private final ExecutorService e;
DelegatedExecutorService(ExecutorService executor) { e = executor; }
public void execute(Runnable command) { e.execute(command); }
public void shutdown() { e.shutdown(); }
public List shutdownNow() { return e.shutdownNow(); }
public boolean isShutdown() { return e.isShutdown(); }
public boolean isTerminated() { return e.isTerminated(); }
public boolean awaitTermination(long timeout, TimeUnit unit)
throws InterruptedException {
return e.awaitTermination(timeout, unit);
}
public Future submit(Runnable task) {
return e.submit(task);
}
public Future submit(Callable task) {
return e.submit(task);
}
public Future submit(Runnable task, T result) {
return e.submit(task, result);
}
public List> invokeAll(Collection> tasks)
throws InterruptedException {
return e.invokeAll(tasks);
}
public List> invokeAll(Collection> tasks,
long timeout, TimeUnit unit)
throws InterruptedException {
return e.invokeAll(tasks, timeout, unit);
}
public T invokeAny(Collection> tasks)
throws InterruptedException, ExecutionException {
return e.invokeAny(tasks);
}
public T invokeAny(Collection> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return e.invokeAny(tasks, timeout, unit);
}
}
/**
* 继承自DelegatedExecutorService的包装类,添加了finalize方法,用于在对象被垃圾回收时调用shutdown方法。
*/
private static class FinalizableDelegatedExecutorService extends DelegatedExecutorService {
FinalizableDelegatedExecutorService(ExecutorService executor) {
super(executor);
}
protected void finalize() {
super.shutdown();
}
}
private SingleThreadExecutorWithExceptions() {}
}