[英]Why shutdownNow() method is able to break for-loop inside Callable object, but fails to do so inside Runnable, unless we specifically call break?
I have this code snippet that runs a single thread executor with two tasks of either Callable, or Runnable type.我有这个代码片段,它运行一个带有两个 Callable 或 Runnable 类型任务的单线程执行器。 Both tasks run a simple for-loop counter with Thread.sleep()
delay per iteration.这两个任务都运行一个简单的 for 循环计数器,每次迭代都有Thread.sleep()
延迟。 The second task starts off immediately after the first task is finished.第二个任务在第一个任务完成后立即开始。 The awaitTermination()
method is supposed to shutdown executor before it manages to complete. awaitTermination()
方法应该在执行程序完成之前关闭它。
I understand that shutdownNow()
calls interrupt()
to a currently executing task as soon as time elapses in awaitTermination()
, somehow managing to immediately break the for-loop inside the first task and aborting the second task if the type of object is MyCallableTask
.我知道,一旦在awaitTermination()
中经过时间, shutdownNow()
就会调用interrupt()
到当前正在执行的任务,如果 object 的类型是MyCallableTask
. Now, if I change the type of submitted objects to MyRunnableTask
the code will also terminate the for-loop as expected, but only because I've included break
in the try-catch block when calling Thread.sleep()
.现在,如果我将提交对象的类型更改为MyRunnableTask
代码也会按预期终止 for 循环,但这只是因为我在调用Thread.sleep()
时在 try-catch 块中包含了break
。
@Override public void run() {
for (int j = 0; j < forLoops; j++) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
break;
}
}
}
If there was no break, the code would have finished the for-loop first and then stop.如果没有中断,代码会先完成 for 循环然后停止。
My question is why don't we need to include any breaks or returns in call()
implementation of MyCallableTask
?我的问题是为什么我们不需要在MyCallableTask
的call()
实现中包含任何中断或返回? I mean, how come the code succeeds to immediately break the for-loop in the call()
method without any additional instructions?我的意思是,代码如何在没有任何额外指令的情况下成功地立即打破call()
方法中的 for 循环?
@Override public Void call() throws Exception {
for (int j = 0; j < forLoops; j++) {
Thread.sleep(sleepTime);
}
return null;
}
InterruptedException
is a checked exception. InterruptedException
是一个检查异常。
You either have to handle it inside the method (with a try/catch), or declare that the method throws it (or a superclass).您要么必须在方法内部处理它(使用 try/catch),要么声明该方法抛出它(或超类)。
Runnable implementations can't throw checked exceptions like InterruptedException
(its base declaration doesn't have throws Exception
or throws InterruptedException
); Runnable 实现不能抛出像InterruptedException
这样的检查异常(它的基本声明没有throws Exception
或throws InterruptedException
); Callable
can, because its declaration does. Callable
可以,因为它的声明可以。
Btw, your Runnable
case should call Thread.currentThread().interrupt()
before the break, so that anything which called that Runnable can also know it was interrupted.顺便说一句,您的Runnable
案例应该在中断之前调用Thread.currentThread().interrupt()
,以便任何调用该 Runnable 的东西也可以知道它被中断了。
You also don't really need the break of you put the try/catch around the loop instead of inside it.您也不需要将 try/catch 放在循环周围而不是循环内部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.