简体   繁体   English

为什么 shutdownNow() 方法能够打破 Callable object 内部的 for-loop,但在 Runnable 内部却不能这样做,除非我们专门调用 break?

[英]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 ?我的问题是为什么我们不需要在MyCallableTaskcall()实现中包含任何中断或返回? 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 Exceptionthrows 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.

相关问题 可在内部循环运行 - Runnable inside for-loop 如何打破在while循环中继续嵌套for循环? - How to break continue a nested for-loop inside a while-loop? 我可以测试for循环内的条件吗,如果满足,请忽略循环内的方法调用? - Can I test for a condition inside a for-loop and if it is met, ignore a method call inside the loop? 如何在 java 的嵌套循环内中断 if 语句 - How to break if statement inside nested loop in java 在方法内部调用可运行对象并传递对象 - Invoke runnable inside a method and pass object 为什么我们不能在Java的普通方法中调用this()方法? - why do we can't call this() method inside a normal method in java? 我们可以在Java中调用对象构造函数内的对象的synchronized方法吗? - Can we call synchronized method of an object inside the constructor of the object in Java? 在while循环中有一个for循环以专门从文件读取? - having a for-loop inside a while-loop to read specifically from file? 在Runnable对象的Run()方法内使用while循环时,线程冻结UI - Thread freezes the UI when using a while loop inside Run() method of Runnable object 如何在while循环中从if条件中断while循环? - How to break a while loop from an if condition inside the while loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM