引言
取消正在执行的任务、线程、服务,并不是那么容易。对于某个任务而言,为了支持取消操作,你不得不增加取消的代码,而且不一定支持阻塞;对于服务而言更是如此,你不清楚服务内部是依赖什么实现,所以就无法正常取消。(例如,内部可能是阻塞队列,可能是多个Task)
任务取消
取消一个任务,或一段程序,最容易想到的操作是增加判断标志,每次执行的时候判断,否则退出程序。但自定义的变量并不能解决阻塞问题。例如下面代码,如果在循环中阻塞了,那么它永远无法判断取消标志。
while(!cancelled) {
do something // block
}
所以Java内部提供了Interrupt机制,它本质上就是一个boolean变量,只是底层(例如阻塞)对该标志有相应的支持,所以中断是实现取消的最合理的方式。
线程与任务
原始的API中,任务和线程通常是耦合在一起的。当我们说起取消任务的时候,实际上是中断运行任务的线程,或者利用中断标志给线程添加取消策略。
这里有一个原则:
除非你知道线程的中断策略,否则不要中断它
原因在于,中断机制本质上是一个协议,它除了设置中断标志之外,并没有做特别的处理,所以每个线程都有自己的中断策略(可能是无策略)。
延迟中断
我们知道Interrupt机制一般需要手动恢复中断,但是在以下情形恢复中断会造成死循环,所以必须采用延迟恢复的技术。
final AtomicBoolean interrupted = new AtomicBoolean(false);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(2);
try {
while (true) {
try {
Integer r = queue.take();
return;
} catch (InterruptedException e) {
// 延迟中断标志
interrupted.set(true);
System.out.println("catch");
e.printStackTrace();
}
}
} finally {
// 恢复中断
if (interrupted.get()){
Thread.currentThread().interrupt();
}
}
}
});
利用Future取消任务,定时取消任务
当我们使用Executor框架的时候,线程是由Executor创建的,我们不能直接去中断线程,因为我们不知道Executor正在运行什么任务。Executor使用一种抽象的机制来管理任务的生命周期,包括获得结果和取消任务。所以,一种更好的方式,就是利用Future的cancel方法来取消任务,这是非常方便的。
由于任务执行时间是不确定的,设定期望执行时间,应该是一种常见的操作,以下提供一种最佳实践。
public void futureTimedRun(Runnable runnable, long timeout, TimeUnit timeUnit) {
Future<?> future = executor.submit(runnable);
try {
future.get(timeout, timeUnit);
} catch (InterruptedException e) {
// 处理中断
e.printStackTrace();
} catch (ExecutionException e) {
// 处理执行时异常
e.printStackTrace();
} catch (TimeoutException e) {
// 超时处理
e.printStackTrace();
} finally {
// 取消不再需要的任务
future.cancel(true);
}
}
作为对比,这里也给出一种不好的做法——利用ScheduledExecutor延迟执行,取消任务。
这里的问题是:
- 违背原则,不知道线程的中断策略,就不能操控它。你不知道它是否响应中断,是否继续执行
- 如果任务执行异常,无法捕获。你也不知道它究竟是执行异常还是超时异常,还是中断
public void timedRun(Runnable runnable, long timeout, TimeUnit timeUnit) {
// 启动线程
final Thread thread = new Thread(runnable);
thread.start();
// 定时取消任务
executor.schedule(new