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); } }