Guava Future

Posted 水田如雅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Guava Future相关的知识,希望对你有一定的参考价值。

 @Test
    public void test2() throws ExecutionException, InterruptedException 
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        Future<String> f1 = executorService.submit(() -> "r1");
        Future<String> f2 = executorService.submit(() -> "r2");
        while (true) 
            if (f1.isDone() & f2.isDone()) 
                System.out.println(f1.get() + "|" + f2.get());
                break;
            
        
    

    @Test
    public void test3() throws ExecutionException, InterruptedException 
        ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
        ListenableFuture<String> f1 = executorService.submit(() -> "r1");
        Futures.addCallback(f1, new FutureCallback<String>() 
            @Override
            public void onSuccess(@Nullable String result) 
                System.out.println("result:" + result);
            

            @Override
            public void onFailure(Throwable t) 

            
        );
        ListenableFuture<String> f2 = executorService.submit(() -> "r2");
        Futures.addCallback(f2, new FutureCallback<String>() 
            @Override
            public void onSuccess(@Nullable String result) 
                System.out.println("result:" + result);
            

            @Override
            public void onFailure(Throwable t) 

            
        );
    

以上是关于Guava Future的主要内容,如果未能解决你的问题,请参考以下文章

Guava Future

text guava Future使用异步调用

面试专栏Guava - ListenableFuture,避免Future获取阻塞问题,增加回调

google Guava包的ListenableFuture解析

从Java Future到Guava ListenableFuture实现异步调用

从Java future 到 Guava ListenableFuture实现异步调用