Java 串行接口调用优化

准备面试总结下

 1.CompletableFuture 

 static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 20, 1000L, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<>(100));

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> task1 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("task1");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 1000;
        }, poolExecutor);

        CompletableFuture<Integer> task2 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("task2");
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 2000;
        }, poolExecutor);


        CompletableFuture<Integer> task3 = CompletableFuture.supplyAsync(() -> {
            try {
                System.out.println("task3");
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 5000;
        }, poolExecutor);
        Integer result1 = task1.get();
        System.out.println(result1);
        Integer result2 = task2.get();
        System.out.println(result2);
        Integer result3 = task3.get();
        System.out.println(result3);
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(task1, task2, task3);
        poolExecutor.shutdown();
        System.out.println("执行完毕");

    }

2.CoutDownLatch 

static HashMap<String, Integer> map = new HashMap<String, Integer>();

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 20, 1000L, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<>(100));
        CountDownLatch countDownLatch = new CountDownLatch(3);


        Future<Integer> task1 = poolExecutor.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("任务1");
                Thread.sleep(1000);
                System.out.println("任务1结束");
                countDownLatch.countDown();
                return 1000;
            }
        });

        Future<Integer> task2 = poolExecutor.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("任务2");
                Thread.sleep(500);
                System.out.println("任务2结束");
                countDownLatch.countDown();
                return 500;
            }
        });

        Future<Integer> task3 = poolExecutor.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("任务3");
                Thread.sleep(2000);
                System.out.println("任务3结束");
                countDownLatch.countDown();
                return 2000;
            }
        });
        map.put("task1", task1.get());
        map.put("task2", task1.get());
        map.put("task3", task1.get());


        System.out.println("线程跑完了");
        countDownLatch.await();
        System.out.println("---------------任务执行结束-------------");
        poolExecutor.shutdown();

    }

3.阻塞获取异步调用结果

   public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        Future<String> submit = executorService.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(1000);
                return "Its done";
            }
        });
        while (true){
            if (submit.isDone()){
                System.out.println(submit.get());
                break;
            }
            System.out.println(submit.isDone());

        }
    }

4.除了上面两个方法 还有 CyclicBarrier,Semaphore也都可以实现,可以自己尝试下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值