本文介绍了方法调用的Future.get块。这真的可取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标记这是重复前请仔细阅读的问题。下面是伪code的片段。我的问题是,下面的code不能战胜并行异步处理的想法。我之所以(在自我向下低)说,这是因为在低于code,主线程将提交在不同的线程中执行的任务。在队列提交任务后,将其上的Future.get()方法块的任务返回的值。我宁愿在主线程中执行,而不是提交给不同的线程,并等待结果的任务。那是什么我获得了在一个新的线程执行任务。我知道,你可以等待一段有限的时间等,但如果我真正关心的结果呢?的问题变得是否存在要执行的多任务差。在我看来,我们只是在做的工作同步。我知道的番石榴库,提供非阻塞监听器的接口。但我想知道如果我的理解是对的Future.get()API正确的。如果它是正确的,为什么是的Future.get旨在阻止从而击败并行处理的全过程?

请注意 - 为了记录在案,我使用Java 6

 公共静态无效的主要(字串[] args){私人ExectorService的ExecutorService = ...未来的未来= executorService.submit(新赎回(){
    公共对象调用()抛出异常{
        的System.out.println(异步可赎回);
        返回赎回结果;
    }
});的System.out.println(的Future.get()=+的Future.get());
}


解决方案

未来为您提供方法 isDone()这是不堵,如果计算完成返回true,否则返回false。

的Future.get()用于检索计算的结果。

您有几个选项:


  • 调用isDone(),如果结果已准备好通过调用get()方法要求它,发现怎么也没有阻挡

  • 块无限期使用get()

  • 块指定超时使用get()

整个未来API 的是那里有简单的方法,从执行并行任务的线程获取值。这可以同步或异步进行,如果你preFER,如上面的子弹所描述

Please read the question carefully before marking this as duplicate. Below is the snippet of the pseudo code. My question is, does the below code not defeat the very notion of parallel asynchronous processing. The reason i (ego at a down low) say this because in the below code , the main thread would submit a task to be executed in a different thread. After submitting the task in the queue, it blocks on Future.get() method for the task to return the value. I would rather have the task executed in the main thread rather than submitting to a different thread and waiting for the results. What is that I gained by executing the task in a new thread. I am aware that you could wait for a limited time etc, but then what if I really care about the result? The problem gets worse if there are multiple tasks to be executed. It seems to me that we are just doing the work synchronously. I am aware of the Guava library which provides a non blocking listener interface. But I am interested to know if my understanding is correct for the Future.get() API. If it is correct, why is the Future.get designed to block thereby defeating the whole process of parallel processing?

Note - For the record, I use JAVA 6

public static void main(String[] args){

private ExectorService executorService = ...

Future future = executorService.submit(new Callable(){
    public Object call() throws Exception {
        System.out.println("Asynchronous Callable");
        return "Callable Result";
    }
});

System.out.println("future.get() = " + future.get());
}
解决方案

Future offers you method isDone()which is not blocking and returns true if computation has completed, or false otherwise.

Future.get() is used to retrieve the result of computation.

You have a couple of options:

  • call isDone() and if the result is ready ask for it by invoking get(), notice how there is no blocking
  • block indefinitely with get()
  • block for specified timeout with get()

The whole Future API thing is there to have easy way obtaining values from threads executing parallel tasks. This can be done synchronously or asynchronously if you prefer, as described in bullets above.

这篇关于方法调用的Future.get块。这真的可取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:12