Guava入门~ListenableFuture

JDK1.5 Future

ExecutorService executor = Executors.newCachedThreadPool();
Future<Integer> future = executor.submit(new Callable<Integer>(){
	public Integer call() throws Exception{
		return service.getCount();
	}
});
//Retrieve the value of comput

获取ListenableFuture接口

ListneningExecutorService executorService =
	MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
ListenableFuture<String> listenableFuture = 
	executorService.submit(new Callable<String>()…);
listenableFuture.addListener(new Runnable() {
	@Override
	public void run() {
		methodToRunOnFutureTaskCompletion();
	}
}, executorService);

完整示例

import com.google.common.util.concurrent.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;


public class ListenableFuturesWithCallbacksTest {

    private ListeningExecutorService executorService;
    private CountDownLatch startSignal;
    private CountDownLatch endSignal;
    private static final int NUM_THREADS = 5;
    private boolean callbackRan;


    @Before
    public void setUp() {
        executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
        startSignal = new CountDownLatch(1);
        endSignal = new CountDownLatch(1);
        callbackRan = false;

    }

    @After
    public void tearDown() {
        executorService.shutdownNow();
    }

    @Test
    public void testRunListenableFutureWithCallback() throws Exception {
        ListenableFuture<String> futureTask = executorService.submit(new Task(startSignal));
        futureTask.addListener(new Runnable() {
            @Override
            public void run() {
                callbackRan = true;
                endSignal.countDown();
            }
        }, executorService);

        endSignal.await();
        assertThat(callbackRan, is(true));
        assertThat(futureTask.get(),is("Task Done"));
    }

    @Test
    public void testRunListenableFutureWithFutureCallbackSuccess() throws Exception {
        ListenableFuture<String> futureTask = executorService.submit(new Task(startSignal));
        FutureCallbackImpl callback = new FutureCallbackImpl();
        Futures.addCallback(futureTask, callback);
        startSignal.countDown();
        endSignal.await();
        assertThat(callback.getCallbackResult(), is("Task Done successfully"));
    }

    @Test
    public void testRunListenableFutureWithFutureCallbackFailure() throws Exception {
        ListenableFuture<String> futureTask = executorService.submit(new Task(null));
        FutureCallbackImpl callback = new FutureCallbackImpl();
        Futures.addCallback(futureTask, callback);
        //startSignal.countDown();  don't call countdown
        endSignal.await();
        assertThat(callback.getCallbackResult(), is("java.lang.NullPointerException"));
    }


    public class FutureCallbackImpl implements FutureCallback<String> {

        private StringBuilder builder = new StringBuilder();

        @Override
        public void onSuccess(String result) {
            builder.append(result).append(" successfully");
            done();

        }

        @Override
        public void onFailure(Throwable t) {
            builder.append(t.toString());
            done();
        }

        private void done() {
            endSignal.countDown();
        }

        public String getCallbackResult() {
            return builder.toString();
        }
    }


    private class Task implements Callable<String> {
        private CountDownLatch start;

        public Task(CountDownLatch start) {
            this.start = start;
        }

        @Override
        public String call() throws Exception {
            this.start.await(1, TimeUnit.SECONDS);
            Thread.sleep(1000);
            return "Task Done";
        }
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值