ThreadPoolExecutor为afterExecution提供了一个扩展点,您可以在其中将作业放回队列的末尾。
public class TaskRepeatingThreadPoolExecutor extends ThreadPoolExecutor { public TaskRepeatingThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); } @Override protected void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); this.submit(r); }}
当然,您将需要做更多的工作来自己实例化它,而无需
ExecutorService使用方便的工厂方法,但是构造函数非常简单,可以轻松完成。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)