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

vr成品网站源码在线观看旅游网站推荐

vr成品网站源码在线观看,旅游网站推荐,网站开发实用技术第2版,网站开发建立目录 CompletableFuture 的详解代码测试配置类的引入Demo1Demo2CompletableFuture的async后缀函数与不带async的函数的区别ThreadPoolTaskExecutor 和 ThreadPoolExecutor 的区别Spring 线程池的使用业务使用多线程的原因场景一:场景二:FutureTask介绍线程池为什么要使用阻塞队…目录 CompletableFuture 的详解代码测试配置类的引入Demo1Demo2CompletableFuture的async后缀函数与不带async的函数的区别ThreadPoolTaskExecutor 和 ThreadPoolExecutor 的区别Spring 线程池的使用业务使用多线程的原因场景一:场景二:FutureTask介绍线程池为什么要使用阻塞队列Spring 常用的线程池的使用序列常规使用异步使用 ​ CompletableFuture 的详解    它就是创建一个异步任务然后在干什么,可以使用多任务组合   创建任务的方法 static CompletableFutureVoid runAsync(Runnable runnable) public static CompletableFutureVoid runAsync(Runnable runnable, Executor executor) public static U CompletableFutureU supplyAsync(SupplierU supplier) public static U CompletableFutureU supplyAsync(SupplierU supplier, Executor executor)然后继续上一段的任务(里面包含了串行ANDOR) 串行 public U CompletableFutureU thenApply(Function? super T,? extends U fn) public U CompletableFutureU thenApplyAsync(Function? super T,? extends U fn) public U CompletableFutureU thenApplyAsync(Function? super T,? extends U fn, Executor executor)public CompletionStageVoid thenAccept(Consumer? super T action); public CompletionStageVoid thenAcceptAsync(Consumer? super T action); public CompletionStageVoid thenAcceptAsync(Consumer? super T action,Executor executor);public CompletionStageVoid thenRun(Runnable action); public CompletionStageVoid thenRunAsync(Runnable action); public CompletionStageVoid thenRunAsync(Runnable action,Executor executor);Function? super T,? extends U T上一个任务返回结果的类型 U当前任务的返回值类型   参数解析 thenApply 方法当一个线程依赖另一个线程时获取上一个任务返回的结果并返回当前任务的返回值。(接收上一阶段任务结果,返回结果)thenAccept方法消费处理结果。接收任务的处理结果并消费处理无返回结果。 (接收上一阶段任务结果,不返回结果)thenRun方法不接收上一阶段任务结果并且无返回值 带有Async默认是异步执行的。这里所谓的异步指的是不在当前线程内执行。 AND public U,V CompletionStageV thenCombineAsync (CompletionStage? extends U other,BiFunction? super T,? super U,? extends V fn,Executor executor); - 上一阶段任务与other任务均执行结束接收两个任务的结果并可获取返回值public U CompletionStageU thenComposeAsync(Function? super T, ? extends CompletionStageU fn,Executor executor); - 使用上一阶段任务的结果返回一个新的CompletableFuture实例更多的参数详解 博客链接   多任务组合   public static CompletableFutureVoid allOf(CompletableFuture?... cfs);public static CompletableFutureObject anyOf(CompletableFuture?... cfs); 代码测试   配置类的引入 yml thread:pool:corePoolSize: 4maxPoolSize: 8workQueue: 25keepAliveTime: 30   config import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor;Configuration ConfigurationProperties(prefix thread.pool) public class AsyncConfig{//核心线程数量大小private int corePoolSize 4;//线程池最大容纳线程数private int maxPoolSize 8;//阻塞队列private int workQueue 25;//线程空闲后的存活时长private int keepAliveTime 30;Bean(asyncTaskExecutor)public Executor getAsyncExecutor() {ThreadPoolTaskExecutor threadPoolTaskExecutor new ThreadPoolTaskExecutor();//核心线程数threadPoolTaskExecutor.setCorePoolSize(corePoolSize);//最大线程数threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);//等待队列threadPoolTaskExecutor.setQueueCapacity(workQueue);//线程前缀threadPoolTaskExecutor.setThreadNamePrefix(taskExecutor-);//线程池维护线程所允许的空闲时间,单位为秒threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveTime);// 线程池对拒绝任务(无线程可用)的处理策略threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());threadPoolTaskExecutor.initialize();return threadPoolTaskExecutor;} }  参数详解 建议看看下面的线程池对拒绝任务(无线程可用)的处理策略 1、corePoolSize核心线程数* 核心线程会一直存活及时没有任务需要执行* 当线程数小于核心线程数时即使有线程空闲线程池也会优先创建新线程处理* 设置allowCoreThreadTimeouttrue默认false时核心线程会超时关闭2、queueCapacity任务队列容量阻塞队列* 当核心线程数达到最大时新任务会放在队列中排队等待执行3、maxPoolSize最大线程数* 当线程数corePoolSize且任务队列已满时。线程池会创建新线程来处理任务* 当线程数maxPoolSize且任务队列已满时线程池会拒绝处理任务而抛出异常4、 keepAliveTime线程空闲时间* 当线程空闲时间达到keepAliveTime时线程会退出直到线程数量corePoolSize* 如果allowCoreThreadTimeouttrue则会直到线程数量05、allowCoreThreadTimeout允许核心线程超时6、rejectedExecutionHandler任务拒绝处理器* 两种情况会拒绝处理任务- 当线程数已经达到maxPoolSize切队列已满会拒绝新任务- 当线程池被调用shutdown()后会等待线程池里的任务执行完毕再shutdown。如果在调用shutdown()和线程池真正shutdown之间提交任务会拒绝新任务* 线程池会调用rejectedExecutionHandler来处理这个任务。如果没有设置默认是AbortPolicy会抛出异常* ThreadPoolExecutor类有几个内部实现类来处理这类情况- AbortPolicy 丢弃任务抛运行时异常默认- CallerRunsPolicy 执行任务- DiscardPolicy 忽视什么都不会发生- DiscardOldestPolicy 从队列中踢出最先进入队列最后一个执行的任务* 实现RejectedExecutionHandler接口可自定义处理器   Demo1    ​   CompletableFutureString task1 CompletableFuture.supplyAsync(() - {System.out.println(洗水壶);try {Thread.sleep(1000);} catch (InterruptedException ex) {ex.printStackTrace();}return 水壶;}).thenApply(e-{System.out.println(烧水);try {Thread.sleep(5000);} catch (InterruptedException ex) {ex.printStackTrace();}return 热水;});//洗水壶-洗水杯-拿茶叶CompletableFutureString task2 CompletableFuture.supplyAsync(() - {System.out.println(洗茶壶);try {Thread.sleep(1000);} catch (InterruptedException ex) {ex.printStackTrace();}return 茶壶;}).thenApply(e-{try {Thread.sleep(2000);} catch (InterruptedException ex) {ex.printStackTrace();}System.out.println(洗水杯);return 水杯;}).thenApply(e-{System.out.println(拿茶叶);return 茶叶;});//泡茶CompletableFutureString task3 task1.thenCombine(task2, (a, b) - {System.out.println(泡茶);return 茶;});String tea task3.join();System.out.println(tea);参数解析 更多的参数详解 博客链接 Demo2   这个测试我就没有写了自己可以看看 问题当查询接口较复杂时候数据的获取都需要远程调用必然需要花费更多的时间。 假如查询文章详情页面需要如下标注的时间才能完成   // 1. 查询文章详情 0.5s // 2. 查询文章博主个人信息 0.5s // 3. 查询文章评论 1s // 4. 查询博主相关文章分类 1s // 5. 相关推荐文章 1s // ...... 上面的描述只是举个例子不要在意这里的查询描述看实际情况使用有些相关的查询我们可以拆分接口实现上面的描述只是为了举例子。   Service public class ArticleService {Autowiredprivate ArticleClient articleClient;Autowiredprivate UserClient userClient;Autowiredprivate ThreadPoolExecutor threadPoolExecutor;public ItemVo load(Long id) {// 1. 查询文章详情 0.5s// 下面的查询需要用到文章对应的发布用户所以这里需要使用CompletableFuture.supplyAsyncCompletableFutureArticleEntity articleCompletableFuture CompletableFuture.supplyAsync(() - {ResponseVoArticleEntity skuEntityResponseVo this.articleClient.getArticleById(id);ArticleEntity articleEntity skuEntityResponseVo.getData();if (articleEntity null) {return null;}itemVo.setId(id);itemVo.setTitle(articleEntity.getTitle());itemVo.setDefaltImage(articleEntity.getDefaultImage());return articleEntity;}, threadPoolExecutor);// 2. 查询文章博主个人信息 0.5s// 这里查询需要依赖文章关联的用户id所以需要使用articleCompletableFuture.thenAcceptAsync()CompletableFutureVoid userCompletableFuture articleCompletableFuture.thenAcceptAsync(articleEntity - {ResponseVoUserEntity categoryResponseVo this.userClient.queryUserInfoById(articleEntity.getUserId());UserEntity userEntity categoryResponseVo.getData();itemVo.setUserInfo(userEntity);}, threadPoolExecutor); // 3. 查询博主相关文章分类 1s// 这里查询需要依赖文章关联的用户id所以需要使用articleCompletableFuture.thenAcceptAsync()CompletableFutureVoid userOtherArticleCompletableFuture articleCompletableFuture.thenAcceptAsync(articleEntity - {ResponseVoListUserAuserOtherArticleEntity categoryResponseVo this.articleClient.queryUserAuserOtherArticleById(articleEntity.getUserId());UserAuserOtherArticleEntity userAuserOtherArticleEntity categoryResponseVo.getData();itemVo.setUserAuserOtherArticleList(userAuserOtherArticleEntity);}, threadPoolExecutor);// 4. 查询文章评论 1s// 不需要依赖其他请求返回值可以使用新的异步对象 CompletableFuture.runAsync()CompletableFutureVoid commentsCompletableFuture CompletableFuture.runAsync(() - {ResponseVoListUserArticleCategoryEntity userArticleCategoryVo this.userClient.queryCommentsByArticleId(id);UserArticleCategoryEntity userArticleCategoryEntity userArticleCategoryVo.getData();itemVo.setUserArticleCategoryList(userArticleCategoryEntity);}, threadPoolExecutor);// 5. 相关推荐文章 1s// 不需要依赖其他请求返回值可以使用新的异步对象 CompletableFuture.runAsync()CompletableFutureVoid relatedArticlesCompletableFuture CompletableFuture.runAsync(() - {ResponseVoListRelatedArticlesEntity userArticleCategoryVo this.articleClient.queryRelatedArticles(id);UserArticleCategoryEntity userArticleCategoryEntity userArticleCategoryVo.getData();itemVo.setUserArticleCategoryList(userArticleCategoryEntity);}, threadPoolExecutor);}// 多任务执行组合 CompletableFuture.allOf()CompletableFuture.allOf(articleCompletableFuture, userCompletableFuture, userOtherArticleCompletableFuture,commentsCompletableFuture, relatedArticlesCompletableFuture).join();return itemVo; }CompletableFuture的async后缀函数与不带async的函数的区别   参考链接 博客链接   结论 不带async的函数的动作比较复杂f的whenComplete的内容由哪个线程来执行取决于哪个线程X执行了f.complete()。但是当X线程执行了f.complete()的时候whenComplete还没有被执行到的时候就是事件还没有注册的时候那么X线程就不会去同步执行whenComplete的回调了。这个时候哪个线程执行到了whenComplete的事件注册的时候就由哪个线程自己来同步执行whenComplete的事件内容。而whenCompleteAsync的场合就简单很多。一句话就是线程池里面拿一个空的线程或者新启一个线程来执行回调。和执行f.complete的线程以及执行whenCompleteAsync的线程无关。ThreadPoolTaskExecutor 和 ThreadPoolExecutor 的区别   参考链接 博客链接 结论 其实最主要的原因很直观ThreadPoolExecutor是一个不受Spring管理生命周期、参数装配的Java类而有了ThreadPoolTaskExecutor的封装线程池才有Spring“内味”。   Spring 线程池的使用   业务使用多线程的原因   目的是面对高并发的时候提高运行速度 场景一: 一个业务逻辑有很多次的循环每次循环之间没有影响比如验证1万条url路径是否存在正常情况要循环1万次逐个去验证每一条URL这样效率会很低假设验证一条需要1分钟总共就需要1万分钟有点恐怖。这时可以用多线程将1万条URL分成50等份开50个线程没个线程只需验证200条这样所有的线程执行完是远小于1万分钟的。   场景二: 需要知道一个任务的执行进度比如我们常看到的进度条实现方式可以是在任务中加入一个整型属性变量(这样不同方法可以共享)任务执行一定程度就给变量值加1另外开一个线程按时间间隔不断去访问这个变量并反馈给用户。总之使用多线程就是为了充分利用cpu的资源提高程序执行效率当你发现一个业务逻辑执行效率特别低耗时特别长就可以考虑使用多线程。 问题不过CPU执行哪个线程的时间和顺序是不确定的即使设置了线程的优先级因此使用多线程的风险也是比较大的会出现很多预料不到的问题一定要多熟悉概念多构造不同的场景去测试才能够掌握! 项目中可以通过: Order() 设置运行的优先级数字越小级别越高FutureTask介绍   参考 博客链接 线程池为什么要使用阻塞队列   阻塞队列可以保证任务队列中没有任务时阻塞获取任务的线程使得线程进入wait 状态释放 cpu 资源当队列中有任务时才唤醒对应线程从队列中取出消息进行执行。 使得在线程不至于一直占用cpu资源。线程执行完任务后通过循环再次从任务队列中取出任务进行执行代码片段如while (task ! null || (task getTask()) ! null) {}。 不用阻塞队列也是可以的不过实现起来比较麻烦而已有好用的为啥不用呢   Spring 常用的线程池的使用   序列 Spring 通过任务执行器TaskExecutor来实现多线程和并发编程使用 ThreadPoolTaskExecutor 实现一个基于线程池的TaskExecutor 还得需要使用 EnableAsync 开启异步并通过在需要的异步方法那里使用注解Async声明是一个异步任务Spring 已经实现的异常线程池  - SimpleAsyncTaskExecutor不是真的线程池这个类不重用线程每次调用都会创建一个新的线程。- SyncTaskExecutor这个类没有实现异步调用只是一个同步操作。只适用于不需要多线程的地方- ConcurrentTaskExecutorExecutor的适配类不推荐使用。如果ThreadPoolTaskExecutor不满足要求时才用考虑使用这个类- SimpleThreadPoolTaskExecutor是Quartz的 SimpleThreadPool 的类。线程池同时被quartz和非quartz使用才需要使用此类- ThreadPoolTaskExecutor 最常使用推荐。 其实质是对 java.util.concurrent.ThreadPoolExecutor 的包装    扩展相信大家在 Java 里面也学过 JUC ,里面有 Java 里面的线程池可以直接去看看 ThreadPoolExecutor 至于为什么有线程池Spring 为什么还有在自己搞一个可以自己去探索Spring 的底层还是 ThreadPoolExecutor 只是它的生命周期不受它控制。   常规使用   // 线程池(config里面的Bean)Autowiredprivate Executor taskExecutor;CallableScyTeacher scy ()- scyTeacherMapper.selectOne(new LambdaQueryWrapperScyTeacher().eq(ScyTeacher::getUsername,test)); FutureTaskScyTeacher commentCallable new FutureTask(scy); FutureMap submit executor.submit(commentCallable); Map map submit.get();异步使用   记得开启异步配置类添加 EnableAsync //开启异步执行package com.melodyjerry.thread;import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service;/*** classname AsyncTaskService* description 异步任务的执行类*/ Service public class AsyncTaskService {Async //异步方法public void executeAsyncTask(Integer i) {System.out.javaprintln(执行异步任务: i);}Async //异步方法public void executeAsyncTaskPlus(Integer i) {System.out.println(执行异步任务1 (i1));} }
http://wiki.neutronadmin.com/news/64033/

相关文章:

  • 深圳市做网站做网站的钱叫什么科目
  • 建设平台网站协议唐山企业做网站
  • 石家庄哪里可以做网站网站页面设计效果图
  • 做网贷网站微站网站
  • 自助手机建站系统网站怎么做留言板
  • 珠宝网站建设的主要方式网站建设师特点
  • 建网站 陕西牛人网络科技2017优秀网站设计欣赏
  • 网站建设需要准备什么北京网络公司哪家好
  • 浠水网站建设网站建设的一般过程包括哪些内容
  • 全网营销推广软件公众号seo排名优化
  • 做书店网站版头海南网站建设制作
  • 只买域名可以做自己的网站嘛店铺推广
  • 有没有教做健身餐的网站自己制作游戏的app
  • 安阳网站自然优化营销模式方案
  • 高碑店市建设局网站南京网站推广费用
  • 南阳网站建设哪家专业极简办公ppt模板下载
  • 二级域名分发网站源码桂林漓江景区游玩攻略
  • 唐山网站建设求职简历苏州高新区网页设计
  • 网站架设教程洛阳建设工程信息网
  • 餐饮系统网站建设企业网站建设jz190
  • 上海品划网络做网站网站规划具体内容
  • phpcms网站模板下载网站开发人员的行业分析
  • 企业网站搜索引擎优化方案wordpress stmp
  • cms建站系统 下载平面设计行业市场分析
  • 网站 粘度后台网站模板 html
  • 做网站小编怎么样网站流量排行
  • 东莞找网站设计seo排名优化收费
  • 手机网站专题导航网站好处
  • 成都网站开发多少钱工程认证网站的建设
  • 什么颜色做网站好看网站开发说明