线程池优雅关闭的哲学
引言
关于并发的哲学,本文将着重强调那些关于线程池优雅关闭的一些技巧,希望对你有所启发。
强制关闭线程池的弊端
对于池化的线程池,如果采用强制关闭的方式将线程池直接关闭,就可能存在上下文消息消息,无法的很好的做到启动时继续从上次进度直接运行。
所以,一般来说使用线程池的关闭,我们一般建议是通过如下两个线程池内置的API采用协作式的方式将打断标识设置为true,让线程按照不同的下面两个方法不同的语义完成打断工作:
//等待所有任务结束后关闭threadPool.shutdown();//即刻关闭所有的任务,返回已提交但是还未开始的任务threadPool.shutdownNow();
阻塞式操作的多线程池中断问题
对于shutdown方法而言,他要求线程完成手里的任务后关闭,这就可能出现阻塞式操作不可中断的情况,我们以一个多生产者单消费者为例来说明这个问题。
假设我们现在有下面这样的多个生产者,一旦队列空间被打满,及时我们可以采用线程的打断操作将这个任务的线程打断,我们也无法很好的处理当前要提交的元素,从而导致元素丢失:
private final BlockingQueue<String> queue;public Producer(BlockingQueue<String> queue, Thread thread) {this.queue = queue;}@Overridepublic void run() {while (true) {try {//任务遇到有界队列被阻塞住,直接打断会导致本次的元素信息丢失queue.put(String.valueOf(System.currentTimeMillis()));} catch (InterruptedException e) {throw new RuntimeException(e);}}}
基于标识的上下文中断
对应我们给出的解决策略可以是在任务中添加一个成员域将中断后未及时处理的成员保存下来,让线程进行后续清理,最后再将中断状态往上层代码栈传递:
private String element;@Overridepublic void run() {boolean interrupted = false;try {while (!interrupted) {String elem = String.valueOf(System.currentTimeMillis());try {//任务遇到有界队列被阻塞住,直接打断会导致本次的元素信息丢失queue.put(elem);} catch (InterruptedException e) {interrupted = true;element = elem;}}} finally {Thread.currentThread().interrupt();}}
基于原子化标识优雅关闭
还有一种基于原子化标识的操作,通过利用阻塞队列当前可用长度提前感知而避免阻塞,由此当线程需要中断时,就能够及时的响应并完成中断:
public void run() {boolean interrupted = false;try {while (!interrupted) {String item = RandomUtil.randomString(10);synchronized (queue) {int remainingCapacity = queue.remainingCapacity();if (--remainingCapacity >= 0) {try {queue.put(item);} catch (InterruptedException e) {interrupted = true;}}}}} finally {Thread.currentThread().interrupt();}}
同理消费者也是通过原子操作来取元素这里就不多赘述了:
@Overridepublic void run() {boolean interrupted = false;try {while (!interrupted) {synchronized (queue) {int remainingCapacity = queue.remainingCapacity();if (--remainingCapacity == 0) {try {queue.take();} catch (InterruptedException e) {interrupted = true;}}}}} finally {Thread.currentThread().interrupt();}}
利用毒丸优雅关闭
还有一种做法也是比较常见的做法,即毒丸,当需要中断线程时用一个协定的标识存入队列中:
@Overridepublic void run() {boolean interrupted = false;try {while (!interrupted) {String item = RandomUtil.randomString(10);synchronized (queue) {int remainingCapacity = queue.remainingCapacity();if (--remainingCapacity >= 0) {try {queue.put(item);} catch (InterruptedException e) {interrupted = true;}}}}} finally {synchronized (queue) {int remainingCapacity = queue.remainingCapacity();if (--remainingCapacity >= 0) {try {queue.put(endElement);} catch (InterruptedException e) {}}}Thread.currentThread().interrupt();}}
同理消费者的也是基于毒丸标识感知异常做到优雅中断:
@Overridepublic void run() {boolean interrupted = false;try {while (!interrupted) {synchronized (queue) {int remainingCapacity = queue.remainingCapacity();if (--remainingCapacity == 0) {try {String element = queue.take();if (endElement.equals(element)) {break;}} catch (InterruptedException e) {interrupted = true;}}}}} finally {Thread.currentThread().interrupt();}}
利用中断状态优化shutdownNow的线程池状态管理
从微观的角度了解了关于线程池中的线程的优雅关闭几种技巧之后,我们再来聊聊线程池维度对于任务的把控。
上文我们了解到shutdown是优雅关闭,而shutdown是近实时且能够返回已提交但是未开始的任务,所以在进行任务关闭时,如果我们希望知晓任务处理进度以及近视时的将所有任务关闭,那么我们就应该使用shutdownNow,获取所有的已提交但是未取消的任务,由此我们就可以获得通过互斥法获得运行时但是被中断的任务了:
List<Runnable> runnableList = threadPool.shutdownNow();try {threadPool.awaitTermination(1, TimeUnit.MINUTES);} catch (InterruptedException e) {throw new RuntimeException(e);}