google guava併發編程

guava併發編程html

package xxx.xxx.xxx;

import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;


public class MultiPackPriceServiceTest {

    /**
     *  演示無異常狀況
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public  void testNormalCase() throws ExecutionException, InterruptedException {
        List<ListenableFuture<Integer>> futures = Lists.newArrayList();
        ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
        for (int i = 0; i < 20; i++) {
            final Integer num = i;
            ListenableFuture<Integer> future = service.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    Thread.sleep(new Random().nextInt(5000));
                    System.out.println("threadId:"+Thread.currentThread().getId() + ",result:" + num * num);
                    return num*num;
                }
            });
            futures.add(future);
        }
        System.out.println("*****************************");
        List<Integer> results = Futures.allAsList(futures).get();
        System.out.println(Arrays.toString(results.toArray()));
    }

    /**
     * 演示異常狀況,allAsList會出現程序阻塞
     * @param args
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public  static void testExceptionCase(String[] args) throws ExecutionException, InterruptedException {
        List<ListenableFuture<Integer>> futures = Lists.newArrayList();
        ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
        for (int i = 0; i < 20; i++) {
            final Integer num = i;
            ListenableFuture<Integer> future = service.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int sleep = new Random().nextInt(5000);
                    Thread.sleep(sleep);
                    if (sleep>2500) {
                        System.out.println("99999999999999");
                        throw new Exception("1111");
                    }
                    System.out.println("threadId:"+Thread.currentThread().getId() + ",result:" + num * num);

                    return num*num;
                }
            });
            futures.add(future);
        }
        System.out.println("*****************************");
        List<Integer> results = Futures.allAsList(futures).get();
        System.out.println(Arrays.toString(results.toArray()));
    }

    /**
     * 演示異常狀況,successfulAsList不會出現程序阻塞
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public  void testExceptionCase() throws ExecutionException, InterruptedException {
        List<ListenableFuture<Integer>> futures = Lists.newArrayList();
        ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(5));
        for (int i = 0; i < 20; i++) {
            final Integer num = i;
            ListenableFuture<Integer> future = service.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    int sleep = new Random().nextInt(5000);
                    Thread.sleep(sleep);
                    if (sleep>2500) {
                        System.out.println("99999999999999");
                        throw new Exception("1111");
                    }
                    System.out.println("threadId:"+Thread.currentThread().getId() + ",result:" + num * num);

                    return num*num;
                }
            });
            futures.add(future);
        }
        System.out.println("*****************************");
        List<Integer> results = Futures.successfulAsList(futures).get();
        System.out.println(Arrays.toString(results.toArray()));
    }
}

回調
http://www.javashuo.com/article/p-rslhzfvi-eo.htmljava

相關文章
相關標籤/搜索