棋牌网站怎么做优化,合肥市网站建设 小程序,视差 长沙做网站,miya1173跳转接口线程池异步的基础知识
详情见#xff1a;https://blog.csdn.net/sinat_32502451/article/details/133039624
线程池执行多任务#xff0c;获取返回值
线程池的 submit()方法#xff0c;可以提交任务#xff0c;并返回 Future接口。 而 future.get()#xff0c;可以获取…线程池异步的基础知识
详情见https://blog.csdn.net/sinat_32502451/article/details/133039624
线程池执行多任务获取返回值
线程池的 submit()方法可以提交任务并返回 Future接口。 而 future.get()可以获取到任务的结果但是get()方法会阻塞阻塞时间过长会占用过多的系统资源。 因此在使用时一般都会用 get(long timeout, TimeUnit unit) 设置超时时间。
//该线程池仅用于示例实际建议使用自定义的线程池
ExecutorService executor Executors.newCachedThreadPool();
FutureString future executor.submit(() - task1);
//阻塞获取返回值2秒超时
String result future.get(2, TimeUnit.SECONDS);不过get(long timeout, TimeUnit unit) 比较适合设置单个任务的超时时间在多任务的情况下哪怕设置了超时时间阻塞的时间也会特别长。 比如有5个任务同时执行每个任务设置2s的超时时间在极端情况下这些任务全部阻塞并超时那总共要耗费的时间可能会达到10s这明显是不能接受的。 如果超时时间设置得太小又可能出现频繁超时。在多任务获取返回值的场景更适合使用 CompletableFuture。
CompletableFuture基础知识
详情见 https://blog.csdn.net/sinat_32502451/article/details/132819472
CompletableFuture多任务异步
CompletableFuture多任务异步不需要返回值的话主要使用 allOf()。
allOf()就是将多个任务汇总成一个任务所有任务都完成时触发。allOf()可以配合get()一起使用。
public static void allOfTest() throws Exception {ExecutorService executorService Executors.newCachedThreadPool();CompletableFutureVoid cf1 CompletableFuture.runAsync(() - System.out.println(cf1 ok.), executorService);CompletableFutureVoid cf2 CompletableFuture.runAsync(() - System.out.println(cf2 ok.), executorService);//将两个任务组装成一个新的任务总共的超时时间为2sCompletableFuture.allOf(cf1, cf2).get(2, TimeUnit.SECONDS);}CompletableFuture获取返回值
只有一个任务时CompletableFuture的使用跟线程池异步有点类似。 主要用到 CompletableFuture.supplyAsync(): 异步处理任务有返回值。 public static void supplyAsyncGet() {//该线程池仅用于示例实际建议使用自定义的线程池ExecutorService executorService Executors.newCachedThreadPool();CompletableFutureString completableFuture CompletableFuture.supplyAsync(()- runTask(), executorService);String result null;try {//获取返回值2秒超时result completableFuture.get(2, TimeUnit.SECONDS);} catch (Exception e) {logger.error(completableFuture.get error., e);}logger.info(result:result);}private static String runTask() {try {//任务耗时。可以分别设置1000和3000看未超时和超时的不同结果。Thread.sleep(1000);} catch (InterruptedException e) {logger.error(runTask error., e);}return taskResult;}CompletableFuture多任务异步获取返回值汇总结果
有几个方法比较关键
supplyAsync(): 异步处理任务有返回值whenComplete()任务完成后触发该方法有返回值。还有两个参数第一个参数是任务的返回值第二个参数是异常。allOf()就是所有任务都完成时触发。allOf()可以配合get()一起使用。
示例如下 /*** 异步多任务。汇总返回值*/public static void allOfGet() {//该线程池仅用于示例实际建议使用自定义的线程池ExecutorService executorService Executors.newCachedThreadPool();//线程安全的list适合写多读少的场景ListString resultList Collections.synchronizedList(new ArrayList(50));CompletableFutureString completableFuture1 CompletableFuture.supplyAsync(() - runTask(result1, 1000), executorService).whenComplete((result, throwable) - {//任务完成时执行。用list存放任务的返回值if (result ! null) {resultList.add(result);}//触发异常if (throwable ! null) {logger.error(completableFuture1 error:{}, throwable);}});CompletableFutureString completableFuture2 CompletableFuture.supplyAsync(() - runTask(result2, 1500), executorService).whenComplete((result, throwable) -{if (result ! null) {resultList.add(result);}if (throwable ! null) {logger.error(completableFuture2 error:{}, throwable);}});ListCompletableFutureString futureList new ArrayList();futureList.add(completableFuture1);futureList.add(completableFuture2);try {//多个任务CompletableFuture[] futureArray futureList.toArray(new CompletableFuture[0]);//将多个任务汇总成一个任务总共耗时不超时2秒CompletableFuture.allOf(futureArray).get(2, TimeUnit.SECONDS);} catch (Exception e) {logger.error(CompletableFuture.allOf Exception error., e);}ListString list new ArrayList(resultList);list.forEach(System.out::println);}private static String runTask(String result, int millis) {try {//此处忽略实际的逻辑用sleep代替//任务耗时。可以分别设置1000和3000看未超时和超时的不同结果。Thread.sleep(millis);} catch (InterruptedException e) {logger.error(supplyAsyncGet error.);}return result;}
相关资料
https://blog.csdn.net/sinat_32502451/article/details/132819472 文章转载自: http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.gpxbc.cn.gov.cn.gpxbc.cn http://www.morning.lnmby.cn.gov.cn.lnmby.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.tgqzp.cn.gov.cn.tgqzp.cn http://www.morning.tgpgx.cn.gov.cn.tgpgx.cn http://www.morning.nccyc.cn.gov.cn.nccyc.cn http://www.morning.rjjys.cn.gov.cn.rjjys.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.qprtm.cn.gov.cn.qprtm.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.pphbn.cn.gov.cn.pphbn.cn http://www.morning.jxgyg.cn.gov.cn.jxgyg.cn http://www.morning.rnlx.cn.gov.cn.rnlx.cn http://www.morning.qgfhr.cn.gov.cn.qgfhr.cn http://www.morning.pqcsx.cn.gov.cn.pqcsx.cn http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.yjmlg.cn.gov.cn.yjmlg.cn http://www.morning.fnssm.cn.gov.cn.fnssm.cn http://www.morning.wjpsn.cn.gov.cn.wjpsn.cn http://www.morning.dlwzm.cn.gov.cn.dlwzm.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.lywys.cn.gov.cn.lywys.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.srgyj.cn.gov.cn.srgyj.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.bpcf.cn.gov.cn.bpcf.cn http://www.morning.ynlbj.cn.gov.cn.ynlbj.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.jbpdk.cn.gov.cn.jbpdk.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.tgfsr.cn.gov.cn.tgfsr.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.bqmhm.cn.gov.cn.bqmhm.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.yqtry.cn.gov.cn.yqtry.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.wynnb.cn.gov.cn.wynnb.cn http://www.morning.sgrdp.cn.gov.cn.sgrdp.cn http://www.morning.cpfx.cn.gov.cn.cpfx.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.yfnjk.cn.gov.cn.yfnjk.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.cfqyx.cn.gov.cn.cfqyx.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.xmhpq.cn.gov.cn.xmhpq.cn http://www.morning.zlkps.cn.gov.cn.zlkps.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn