java - 异常不是 Callable 接口(interface)的缺点吗?

标签 java multithreading callable

  1. 为什么当我们使用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/

相关文章:

java - JDialog内有多个JPanel,如何控制面板调整大小顺序?

Java : What happens if a Runnable that is being used in a thread is set to null?

rust - 为什么对实现 Fn 的类型的引用不被识别为可调用?

java - 如何在Java中声明Callable来执行返回void的函数?

java - 使用方法时获得不需要的结果 "<package>.<class>@55f96302"

java - 将 Hibernate 与动态 Eclipse 插件一起使用

java - 上下文注入(inject)的 SecurityContext 为 null

java - 如何知道我的每个java程序是否运行在自己不同的JVM实例上?

ios - 在后台线程中处理 JPEG 图像

java execturorService - 提交任务并捕获异常