当前位置: 首页 > news >正文

会员管理系统功能介绍seo教程自学入门教材

会员管理系统功能介绍,seo教程自学入门教材,wordpress付费下载功能,网页设计与制作的论文当我们在项目中遇到很多业务同时处理#xff0c;如果是串行肯定是影响性能的#xff0c;这时候就需要异步执行了#xff0c;说道异步肯定就有很多方案了 方案一#xff1a; 比如使用spring的异步注解#xff0c;比如下面的代码,每个方法上面都是异步注解#xff0c;当时…当我们在项目中遇到很多业务同时处理如果是串行肯定是影响性能的这时候就需要异步执行了说道异步肯定就有很多方案了 方案一 比如使用spring的异步注解比如下面的代码,每个方法上面都是异步注解当时这种方案很多不足点首先就是他的显示创建线程无法实现线程复用然后就是无法统一处理异常及任务是否执行完了 class void test(){test01();test02();test03(); }方案二 使用线程池 CountDownLatch 使用 线程池可以实现线程复用肯定是异步执行的不二选择 使用CountDownLatch您可以在每个任务完成时递减 CountDownLatch然后主线程等待 CountDownLatch 的计数为零以确定所有任务都已经完成。这需要一些额外的编程工作但允许更灵活的控制 int numberOfTasks 10; ExecutorService executorService Executors.newFixedThreadPool(5); CountDownLatch countDownLatch new CountDownLatch(numberOfTasks);for (int i 0; i numberOfTasks; i) {executorService.submit(() - {// 执行任务countDownLatch.countDown();}); }try {countDownLatch.await();System.out.println(所有任务已经执行完毕); } catch (InterruptedException e) {System.err.println(等待被中断); } executorService.shutdown(); awaitTermination 使用awaitTermination方法ExecutorService 接口提供了 awaitTermination 方法该方法允许您等待一段时间来检查线程池中的任务是否已经执行完。例如 ExecutorService executorService Executors.newFixedThreadPool(5); // 提交任务到线程池executorService.shutdown(); // 停止接受新任务 try {if (executorService.awaitTermination(10, TimeUnit.SECONDS)) {System.out.println(所有任务已经执行完毕);} else {System.out.println(等待超时仍有任务未执行完);} } catch (InterruptedException e) {System.err.println(awaitTermination被中断); } invokeAll ExecutorService executorService Executors.newFixedThreadPool(5); ListCallableVoid tasks new ArrayList(); // 添加任务到tasksListFutureVoid futures executorService.invokeAll(tasks); for (FutureVoid future : futures) {if (!future.isDone()) {System.out.println(仍有任务未执行完);break;} } executorService.shutdown(); 方案三 既然使用了线程池能否再优化下呢使用java8 毕竟流行的异步编程CompletableFuture 比如 import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException;public class CompletableFutureExample {public static void main(String[] args) {CompletableFutureVoid future1 CompletableFuture.runAsync(() - {此处可以将任务结果存放在一个集合或者一个对象中// 执行任务1System.out.println(Task 1 is running...);});CompletableFutureVoid future2 CompletableFuture.runAsync(() - {// 执行任务2此处可以将任务结果存放在一个集合或者一个对象中System.out.println(Task 2 is running...);});CompletableFutureVoid future3 CompletableFuture.runAsync(() - {// 执行任务3此处可以将任务结果存放在一个集合或者一个对象中System.out.println(Task 3 is running...);});CompletableFutureVoid allOf CompletableFuture.allOf(future1, future2, future3);try {allOf.get(); // 等待所有任务完成System.out.println(All tasks are completed.);} catch (InterruptedException | ExecutionException e) {e.printStackTrace();}} } 但是这种写法也不够优雅因为每个异步任务都需要把结果存放出来 可以优化下 private ListObject asyncHandleTask(MapString, TaskHandlerService handleServiceMap, MapString, TaskResultParserDTO taskResultParserMap) {//提交任务ListCompletableFutureListObject completableFutureList handleServiceMap.entrySet().stream().map(entry - CompletableFuture.supplyAsync(() - entry.getValue().handle(taskResultParserMap.get(entry.getKey())), threadPoolTaskExecutor)).collect(Collectors.toList());//等待所有任务完成CompletableFutureVoid allTaskFeatureList CompletableFuture.allOf(completableFutureList.toArray(new CompletableFuture[0]));//等待所有任务完成或者CompletableFutureVoid[] futures new CompletableFuture[]{future1, future2, future3};CompletableFutureVoid allOf CompletableFuture.allOf(futures); //等待所有任务完成或者CompletableFutureVoid allTaskFeatureList CompletableFuture.allOf(completableFutureList.toArray(new CompletableFuture[completableFutureList.size]));//获取所有任务的结果数据CompletableFutureListListObject listCompletableFuture allTaskFeatureList.thenApply(v - completableFutureList.stream().map(CompletableFuture::join).collect(Collectors.toList()));ListListObject resultList;try {resultList listCompletableFuture.get();} catch (InterruptedException | ExecutionException e) {log.error(处理任务结果数据失败{}, ExceptionUtils.getStackTrace(e));throw new CustomException(TaskProcessStatusEnum.WAIT_PARSER_RESULT_FAIL);}//整合数据return resultList.stream().flatMap(Collection::stream).collect(Collectors.toList());}
http://www.tj-hxxt.cn/news/142176.html

相关文章:

  • 做网站前端的软件wordpress 捐助
  • 做电影网站会有什么惩罚本溪建设网站
  • 自己做网站都要什么手续福田网站优化
  • 有没有做字的网站怎样开电商线上店
  • 清城网站seo快站建站教程
  • 成都专业网站设计公司国家城乡和建设厅特殊工种网站
  • 大学生毕业设计网页设计sem 优化价格
  • 做网站无锡吉林市建设局网站
  • 连云港企业网站制作网络广告推广好的有哪些
  • 曰本做爰视频网站网站关键词查询
  • 旅游建设门户网站的方案网站建设 展滔科技大厦
  • 小区媒体网站建设专业做生鲜的网站
  • 北京建设网站专家怎么开网店?
  • 动图在线制作网站黄石城乡建设网站
  • 电子商务网站制作徐州手机网站优化公司
  • 网站开发亿玛酷专注4wordpress新奇插件
  • 青海西宁做网站多少钱最新手机网站推荐
  • 名片式网站模板wordpress健康主题
  • 长春建设局网站处长crm管理系统排名
  • 界面做的比较好的网站网上购物商城建设
  • 网站建设的内容管理怎么做网站支付
  • 网站设计与实现毕业设计电商类网站咋做
  • 做全网营销型网站建设常州建设工程质量监督网站
  • 东软网站建设保定网站设计制作
  • 自己网站给别人网站做外链网页设计与网站建设设计报告
  • 伊利网站建设水平评价如何自己做网站做淘宝客
  • 网站建设中 优秀账户的标准旅游网站建设与网页设计
  • 织梦网站建设视频wordpress主题添加
  • 广东门户网站建设google搜索入口
  • 网站模板 首饰预定襄阳市住房城乡建设部网站