【发布时间】:2021-03-19 03:11:57
【问题描述】:
我正在尝试一个 java 程序来理解 Future 的工作原理。
我编写了以下程序,但它永远不会结束。如果我在 Thread.sleep() 中输入一个小于 10 的值,那么它可以工作,但不适用于值 >=10。
我了解导致问题的部分可能是 future.get 调用。
但是,经过进一步分析,我尝试的是处理所有异常,而不是让 jvm 处理它们。
现在它终止了。
我做了进一步的检查,发现如果我抛出 ExecutionException 和 InterruptedException 并处理 TimeoutException 它再次正常工作。
这里奇怪的部分是我必须强制处理 TimeoutException,否则它将不起作用。我不太清楚为什么这种奇怪的行为会持续存在。
我使用的是 OpenJDK 15。
如果有人想尝试代码 sn-p 在这里:
import java.util.concurrent.*;
public class FixedThreadPoolExecutorDemo2 {
public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
ExecutorService executorService = Executors.newFixedThreadPool(2);
workWithFutureCallable(executorService);
executorService.shutdownNow();
}
private static void workWithFutureCallable(ExecutorService executorService) throws ExecutionException, InterruptedException, TimeoutException {
Future<Integer> myOtherFuture = executorService.submit(() -> {
try {
Thread.sleep(109);
} catch (InterruptedException e) {
}
return 1000;
});
System.out.println("myOtherFuture should be cancelled if running for more than specified time. ->" + myOtherFuture.get(10, TimeUnit.MILLISECONDS));
}
}
【问题讨论】:
-
当异常被传递时,执行器服务没有被关闭并且它正在使用一个非守护线程。
newFixedThreadPool的文档:“池中的线程将一直存在,直到它被显式关闭”。使用(创建)一个ThreadFactory创建守护进程Threads 或(更好的 IMO)使用try-finally始终关闭 -
` executorService.shutdownNow();` 会在代码中调用。
-
你必须能够在 main 方法中捕捉到线程中断并且应该退出应用程序。
-
另外,我看到它进入线程组的 uncaughtException 方法,如果没有看到 instanceof
ThreadDeath,它会打印堆栈跟踪并移动 ahed
标签: java concurrency future