Asynchronous Google Guava中函数和异步函数的区别是什么?

Asynchronous Google Guava中函数和异步函数的区别是什么?,asynchronous,guava,Asynchronous,Guava,在内部,函数和异步函数都是非阻塞的(对吗?)。那么,为什么我们有异步函数呢。只是一个返回ListenableFuture对象和另一个返回对象的区别吗?例如,其中一个处理Iterable的转换,另一个处理从一个ListenableFuture到另一个的转换(可能是异步的)。这些概念是完全不同的,处理不同的事情 这里是AsyncFunction的一个小示例,基本上我们异步获取一个字符串,然后异步将该字符串转换为另一个字符串。不过,这只是一个样本 public class GuavaAsyncFunc

在内部,函数和异步函数都是非阻塞的(对吗?)。那么,为什么我们有异步函数呢。只是一个返回ListenableFuture对象和另一个返回对象的区别吗?

例如,其中一个处理Iterable的转换,另一个处理从一个ListenableFuture到另一个的转换(可能是异步的)。这些概念是完全不同的,处理不同的事情

这里是AsyncFunction的一个小示例,基本上我们异步获取一个字符串,然后异步将该字符串转换为另一个字符串。不过,这只是一个样本

public class GuavaAsyncFunction {
    public static void main(String[] args) {

        ExecutorService deletegate = Executors.newFixedThreadPool(1);
        ExecutorService deletegateForAsyncFunction = Executors.newFixedThreadPool(1);

        ListeningExecutorService pool = MoreExecutors.listeningDecorator(deletegate);
        ListeningExecutorService poolForAsyncFunction = MoreExecutors.listeningDecorator(deletegateForAsyncFunction);
        ListenableFuture<String> resultFromWorker = pool.submit(new Worker());

        ListenableFuture<String> finalResult = Futures.transform(resultFromWorker, new AsyncTransformation(poolForAsyncFunction));

        Futures.addCallback(finalResult, new MyFutureCallback());

    }

    private static final class Worker implements Callable<String> {
        public String call() throws Exception {
            try {
                System.out.println("Executing in thread="+Thread.currentThread().getName());
                //simultate some work
                TimeUnit.SECONDS.sleep(3);
            } catch(InterruptedException ex){
                ex.printStackTrace();
            }
            return "CALCULATED_VALUE";
        }
    }

    /**
     * Almost like Function transformation but it is asynchronous
     */
    private static final class AsyncTransformation implements AsyncFunction<String, String> {

        private final ListeningExecutorService poolToRunFunctionIn;

        public AsyncTransformation(ListeningExecutorService poolToRunFunctionIn){
            this.poolToRunFunctionIn = poolToRunFunctionIn;
        }

        public ListenableFuture<String> apply(String input) throws Exception {
            return poolToRunFunctionIn.submit(new FunctionWorker(input));
        }

        /**
         * 'worker' for the AsyncFunction
         */
        private static final class FunctionWorker implements Callable<String> {
            private final String input;
            public FunctionWorker(String input){
                this.input = input;
            }
            public String call() throws Exception {
                try {
                    System.out.println("Executing in thread="+Thread.currentThread().getName());
                    TimeUnit.SECONDS.sleep(3);
                } catch(InterruptedException ex){
                    ex.printStackTrace();
                }
                return input + "_TRANSFORMED";
            }
        }
    }

    /**
     * what do to when the ListenableFuture has been processed
     */
    private static final class MyFutureCallback implements FutureCallback<String> {
        public void onSuccess(String result) {
            System.out.println("Result from computation = " + result);
        }

        public void onFailure(Throwable t) {
            t.printStackTrace();
        }
    }
}
公共类函数{
公共静态void main(字符串[]args){
ExecutorService deletegate=Executors.newFixedThreadPool(1);
ExecutorService deletegateForAsyncFunction=Executors.newFixedThreadPool(1);
ListingExecutor服务池=MoreExecutors.ListingDecorator(deletegate);
ListingExecutor服务poolForAsyncFunction=MoreExecutors.ListingDecorator(deletegateForAsyncFunction);
ListenableFuture resultFromWorker=pool.submit(new Worker());
ListenableFuture finalResult=Futures.transform(resultFromWorker,新的异步转换(poolForAsyncFunction));
Futures.addCallback(finalResult,newMyFutureCallback());
}
私有静态最终类辅助程序实现可调用{
公共字符串调用()引发异常{
试一试{
System.out.println(“在线程中执行=“+thread.currentThread().getName());
//模拟一些工作
时间单位。秒。睡眠(3);
}捕获(中断异常例外){
例如printStackTrace();
}
返回“计算值”;
}
}
/**
*几乎类似于函数转换,但它是异步的
*/
私有静态最终类AsyncTransformation实现AsyncFunction{
私人最终登录ExecutorService poolToRunFunctionIn;
公共异步转换(ListingExecutorService poolToRunFunctionIn){
this.poolToRunFunctionIn=poolToRunFunctionIn;
}
public ListenableFuture应用(字符串输入)引发异常{
return poolToRunFunctionIn.submit(新函数工作者(输入));
}
/**
*异步函数的“worker”
*/
私有静态最终类FunctionWorker实现可调用{
私有最终字符串输入;
公共FunctionWorker(字符串输入){
这个输入=输入;
}
公共字符串调用()引发异常{
试一试{
System.out.println(“在线程中执行=“+thread.currentThread().getName());
时间单位。秒。睡眠(3);
}捕获(中断异常例外){
例如printStackTrace();
}
返回输入+“_转换”;
}
}
}
/**
*处理ListenableFuture后,您将如何处理
*/
私有静态最终类MyFutureCallback实现FutureCallback{
成功时的公共void(字符串结果){
System.out.println(“计算结果=”+结果);
}
失效时的公共无效(可丢弃的t){
t、 printStackTrace();
}
}
}

我怀疑这就是它的工作原理。谢谢你证实我的怀疑。