啥?

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
public class Executor {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 12,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(5), new ThreadPoolExecutor.CallerRunsPolicy());

        Map paramMap=new HashMap();
        
        //在进行条件初始化时(format时)可以使用线程池
        //初始化条件1
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            paramMap.put("p1","你若运行完了等我");
        }, executor);

        //初始化条件2
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            paramMap.put("p2","你若运行完了等我");
        }, executor);

        //让两个线程都等各自执行完毕
        CompletableFuture<Void> allFutureWait = CompletableFuture.allOf(future1, future2);
        Void unused = allFutureWait.get();

        System.out.println("paramMap="+paramMap);
    }
}

 

posted on 2022-04-06 09:45  啥?  阅读(43)  评论(0编辑  收藏  举报