简体   繁体   English

Java Callable:调用get()之前线程会发生什么

[英]Java Callable : What happens to the thread before get() is called

More precisely : If I start async computation by calling submit(Callable<T> task) method on an ExecutorService (itself constructed with Executors.newCachedThreadPool() ), I can wait for the computation to finish and retrieve the result by calling Future.get() . 更确切地说:如果我通过在ExecutorService (它自己用Executors.newCachedThreadPool()构造submit(Callable<T> task)上调用submit(Callable<T> task)方法来启动异步计算,我可以等待计算完成并通过调用Future.get()来检索结果。 Future.get()

My question is : if the computation is already finished, what happens until I call get() ? 我的问题是:如果计算已经完成,那么在调用get()之前会发生什么? Does the thread is blocked until I retrieved the result? 在检索结果之前,线程是否被阻塞? Does the result is stored and the thread assigned to another task? 是否存储结果并将线程分配给另一个任务? Something completely different? 完全不同的东西?

Thanks in advance for you answers 在此先感谢您的答案

No, Thread is not blocked, it is returned to the pool. 不,Thread没有被阻塞,它被返回到池中。 In general it is the get() calling thread dependant on worker, not the other way around. 通常, get()调用线程依赖于worker,而不是相反。 So if there is a result, return it, if not, wait until it will be available. 因此,如果有结果,则返回,如果没有,请等待它可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM