尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

Java多线程-CompletableFuture

Java多线程-CompletableFuture ExecutorServiceexecutorServiceExecutors.newFixedThreadPool(8);CompletableFutureIntegerfirstFutureCompletableFuture.supplyAsync(()-{log.info(开始执行第一个任务);try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}return1;},executorService);CompletableFutureIntegersecondFutureCompletableFuture.supplyAsync(()-{log.info(开始执行第二个任务);try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}return2;},executorService);CompletableFutureIntegerthirdFutureCompletableFuture.supplyAsync(()-{log.info(开始执行第三个任务);try{Thread.sleep(1000);}catch(InterruptedExceptione){e.printStackTrace();}return3;},executorService);try{IntegerfirstfirstFuture.get();log.info(first:{},first);IntegersecondsecondFuture.get();log.info(second:{},second);IntegerthirdthirdFuture.get();log.info(third:{},third);}catch(Exceptione){e.printStackTrace();}未完待续。。。参考https://mp.weixin.qq.com/s/VCRtwaJ99p5FzBl0xlClIgtry{CompletableFuture.allOf(firstFuture,secondFuture,thirdFuture).join();}catch(Exceptione){e.printStackTrace();}System.out.println(执行完成);
返回列表