- 为什么当我们使用callable创建线程时需要FutureTask实例?
2.我可以看到,使用 callbale 我们可以使用 futuretask.get() 方法获取异常(如果发生)。所以在这种情况下,我们的主线程应该等待直到线程完成!不会破坏线程的灵魂目的吗?这难道不是 Callable 的缺点吗?
最佳答案
V get() throws InterruptedException,ExecutionException
Waits if necessary for the computation to complete, and then retrieves its result.
现在
Why is that we need FutureTask instance when we make thread using callable?
如果您需要可调用的结果,那么您应该使用它
our Main thread should be waiting till the thread is completed!
是的,所以除非你真的需要结果,否则你应该避免使用它
您还可以使用overloaded version因此,等待不会无限期,并且一旦 wait()
超时,您就会收到超时异常
关于java - 异常不是 Callable 接口(interface)的缺点吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29160752/