Java并发编程 - 无尽的循环
我正在阅读本书介绍游戏框架:Java Web应用程序开发(ISBN 978-1-4842-5645-9),在callable
:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallableClient {
/**
* @param args
*/
// Step1 : Create a Runnable
public static void main(String[] args) {
Callable callableTask = new CallableTask();
// Step 2: Configure Executor
// Uses FixedThreadPool executor
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> future = executor.submit(callableTask);
boolean listen = true;
while (listen) {
if (future.isDone()) {
String result;
try {
result = future.get();
listen = false;
System.out.println(result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
executor.shutdown();
}
}
上有此示例。我的问题是,如果Future
投掷和异常的计算会在循环永远运行时?
我认为,它将永远循环。
首先,如果未来
计算会引发异常,则
future.isdone()
始终评估true
。因此,我们到达的内部,可以在其中设置停止条件。还可以。
第二,根据未来, ,当达到行
result = future.get();
时,它将始终抛出executionException
,因为计算会引发异常。此例外是在try-catch
块中捕获的,而无需达到停止条件先决条件,即
listan = false = false
最后,以上将创建一个无尽的循环。
我的假设中有什么错误,还是示例的作者真的错了?
I am reading the book Introducing Play Framework: Java Web Application Development (ISBN 978-1-4842-5645-9) and there is this example on Callable
:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallableClient {
/**
* @param args
*/
// Step1 : Create a Runnable
public static void main(String[] args) {
Callable callableTask = new CallableTask();
// Step 2: Configure Executor
// Uses FixedThreadPool executor
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> future = executor.submit(callableTask);
boolean listen = true;
while (listen) {
if (future.isDone()) {
String result;
try {
result = future.get();
listen = false;
System.out.println(result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
executor.shutdown();
}
}
My question is, if the computation of the Future
throws and exception, will the while
loop run forever?
In my opinion, yes, it will loop forever.
First, if the Future
computation throws an exception, then
future.isDone()
always evaluates to true
. So we reach the inside of the if
, where the stopping condition can be set. Still Ok.
Second, as per future documentation, when reaching line
result = future.get();
it will always throw ExecutionException
because the computation threw an exception. This exception is caught in the try-catch
block, without reaching the stopping condition precondition, which is
listen = false
Lastly, the above will create an endless cycle.
Is there any mistake in my assumptions or is the author of the example really wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面提到的程序循环永远,如果可可的例外。
这是一个带有可可的代码段,可引发异常。执行编译的片段循环。
The program mentioned above cycles forever, if the callable throws an exception.
This is a code snippet with the callable that throws an exceptions. Executing compiled snippet loops forever.