如果我使用Futures
喜欢
List<Future<String>> results = executorService.invokeAll(tasks);
或者
Future<String> res = executorService.submit(new SingleWorker());
System.out.println(res.get());
系统等待任务完成。
即使我executorService.shutdownNow();
在上面提到的语句之后,我真的不明白系统何时会强制终止文档中提到的现有线程,因为系统在任务完成并返回未来之前永远不会到达线路。我错过了什么吗?是否有不同的测试用例场景来测试它?
shutdownNow
仅当我们Runnable
说 ie 时才
有效?executorService.submit(new MyRunnable())
编辑:
我尝试了一些不同的东西,发现
a)shutdownNow
不适用于invokeAll
.
b)shutdownNow
如果在此之后存在,Future.get
则该语句shutdownNow
被阻塞,直到 Future
被解决(在 的情况下Callable
)。
c)shutdownNow
完美地与Runnable
.
以下是我为测试而编写的代码:
class SingleRunnableWorker implements Runnable {
@Override
public void run() {
System.out.println("SingleRunnableWorker Running..");
try {
Thread.sleep(10000);
System.out.println("SingleRunnableWorker Running after sleep..");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class SingleCallableWorker implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("SingleCallableWorker Calling..");
Thread.sleep(10000);
System.out.println("SingleCallableWorker Calling after sleep..");
return "SingleCallableWorker Calling done";
}
}
我正在测试它如下:
ExecutorService executorService = Executors.newFixedThreadPool(4);
/*List<Future<String>> results = */executorService.invokeAll(tasks);//blocks by default
Future<String> res = executorService.submit(new SingleCallableWorker());
//System.out.println(res.get()); //blocks if uncommented
executorService.submit(new SingleRunnableWorker());
executorService.shutdownNow();
任务都在哪里Callables
。
底线是invokeAll
并且Future.get
正在阻止操作。有人可以验证吗?