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

免费网站推广工具有哪些企业管理培训视频免费

免费网站推广工具有哪些,企业管理培训视频免费,wordpress首页显示推荐标志,论坛网站开发费用前言 Async这个注解想必大家都用过#xff0c;是用来实现异步调用的。一个方法加上这个注解以后#xff0c;当被调用时会使用新的线程来调用。但其实这里面也有一个坑。 问题 这个注解使用时存在如下问题#xff1a;在没有自定义线程池的场景下#xff0c;默认会采用Sim…前言 Async这个注解想必大家都用过是用来实现异步调用的。一个方法加上这个注解以后当被调用时会使用新的线程来调用。但其实这里面也有一个坑。 问题 这个注解使用时存在如下问题在没有自定义线程池的场景下默认会采用SimpleAsyncTaskExecutor创建线程线程池的最大大小为Integer的MAX_VALUE相当于调用一次创建一个线程缺乏线程重用机制。在并发大的场景下可能引发严重性能问题。下面是他的源代码 /*** {link TaskExecutor} implementation that fires up a new Thread for each task,* executing it asynchronously.** pSupports limiting concurrent threads through the concurrencyLimit* bean property. By default, the number of concurrent threads is unlimited.** pbNOTE: This implementation does not reuse threads!/b Consider a* thread-pooling TaskExecutor implementation instead, in particular for* executing a large number of short-lived tasks.*/ public class SimpleAsyncTaskExecutor extends CustomizableThreadCreatorimplements AsyncListenableTaskExecutor, Serializable {//省略不重要的方法Overridepublic void execute(Runnable task, long startTimeout) {Assert.notNull(task, Runnable must not be null);Runnable taskToUse (this.taskDecorator ! null ? this.taskDecorator.decorate(task) : task);if (isThrottleActive() startTimeout TIMEOUT_IMMEDIATE) {this.concurrencyThrottle.beforeAccess();doExecute(new ConcurrencyThrottlingRunnable(taskToUse));}else {doExecute(taskToUse);}}/*** 模板方法用于实际执行任务.* p默认实现创建一个新线程并启动它*/protected void doExecute(Runnable task) {//如果threadFactory为空则直接创建线程执行。Thread thread (this.threadFactory ! null ? this.threadFactory.newThread(task) : createThread(task));thread.start();}}那么如何解决这个问题呢可以采用下面的方法 自定义线程池 有如下几种方式可以配置线程池一种配置默认线程池让所有Async自动共享或者配置单独的线程池使用Async时指定线程池。 使用配置文件中配置默认线程池 application.properties参考配置yml文件同理。 # 线程池创建时的初始化线程数默认为8 spring.task.execution.pool.core-size1 # 线程池的最大线在这里插入代码片程数默认为int最大值 spring.task.execution.pool.max-size1 # 用来缓冲执行任务的队列默认为int最大值 spring.task.execution.pool.queue-capacity10 # 线程终止前允许保持空闲的时间 spring.task.execution.pool.keep-alive60s # 是否允许核心线程超时 spring.task.execution.pool.allow-core-thread-timeouttrue # 是否等待剩余任务完成后才关闭应用 spring.task.execution.shutdown.await-terminationfalse # 等待剩余任务完成的最大时间 spring.task.execution.shutdown.await-termination-period # 线程名的前缀设置好了之后可以方便我们在日志中查看处理任务所在的线程池 spring.task.execution.thread-name-prefixasynctask-通过实现接口配置默认线程池 实现AsyncConfigurer覆盖getAsyncExecutor()方法。注意这个方法的优先级比配置文件高。 Configuration EnableAsync public class AsyncConfig implements AsyncConfigurer {public Executor getAsyncExecutor() {ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();executor.setCorePoolSize(3); //核心线程数executor.setMaxPoolSize(3); //最大线程数executor.setQueueCapacity(1000); //队列大小executor.setKeepAliveSeconds(600); //线程最大空闲时间executor.setThreadNamePrefix(async-Executor-); //指定用于新创建的线程名称的前缀。executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略一共四种此处省略// 这一步千万不能忘了否则报错 java.lang.IllegalStateException: ThreadPoolTaskExecutor not initializedexecutor.initialize();return executor;} }单独配置线程池使用Async指定线程池 这种方式可以给每个async的方法指定单独的线程池但缺点是开发得知道怎么去设置。 /** * 独立线程池配置 */ Configuration public class TaskExecutorConfig {Beanpublic TaskExecutor taskExecutor() {ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();// 设置核心线程数executor.setCorePoolSize(1);// 设置最大线程数executor.setMaxPoolSize(1);// 设置队列容量executor.setQueueCapacity(20);// 设置线程活跃时间秒executor.setKeepAliveSeconds(60);// 设置默认线程名称executor.setThreadNamePrefix(task-);// 设置拒绝策略executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());// 等待所有任务结束后再关闭线程池executor.setWaitForTasksToCompleteOnShutdown(true);return executor;} }public class AsyncService {Async(taskExecutor)public void task1() throws InterruptedException {TimeUnit.SECONDS.sleep(1L);log.info(task1 complete);}Async(taskExecutor)public void task2() throws InterruptedException {TimeUnit.SECONDS.sleep(2L);log.info(task2 complete);}Async(taskExecutor)public void task3() throws InterruptedException {TimeUnit.SECONDS.sleep(3L);log.info(task3 complete);} }下面是测试代码大家可以用这个代码分别测试上述3种方式。 RestController RequestMapping(/async) public class AsyncController {AutowiredAsyncService asyncService;RequestMapping(/test)public String test() throws InterruptedException {asyncService.task1();asyncService.task2();asyncService.task3();return success;} }Service Slf4j public class AsyncService {Asyncpublic void task1() throws InterruptedException {TimeUnit.SECONDS.sleep(1L);log.info(task1 complete);}Asyncpublic void task2() throws InterruptedException {TimeUnit.SECONDS.sleep(2L);log.info(task2 complete);}Asyncpublic void task3() throws InterruptedException {TimeUnit.SECONDS.sleep(3L);log.info(task3 complete);} }
http://www.dnsts.com.cn/news/81242.html

相关文章:

  • 北京网站建设方案品牌公司网站打模块
  • 网络设计网站南通网站维护
  • 重庆多语网站建设品牌企业自己使用原生php做网站性能
  • 天津网站开发网站南宁网站改版
  • 关于网站建设的大学做微信平台网站
  • 网站中文名注册网页设计与开发第四版答案
  • 公司网站页面设计图片wordpress侧边栏插件
  • vue.js网站开发用例湖北企业建站系统平台
  • 精品网站建设哪家公司服务好phpwind 做的网站
  • 做网单哪个网站最好用设计非常好的网站
  • 彩票网站开发需要多少钱网站优化主要怎么做
  • 企业网站公众号mvc 网站开发
  • 朝阳区北京网站建设wordpress后台轮播图设置
  • 静态网站需要数据库吗马大云湘潭
  • 网站建设2000字论文企业形象设计教案
  • 网站建设网站的好处网站建设项目成本估算表
  • 做受免费网站搜索引擎营销的名词解释
  • 电子商务网站建设与规划教案wordpress禁止前台登录
  • 南平高速建设有限公司网站济南品牌网站制作便宜
  • flash网站开源品牌vi
  • CMS源码就可以做网站吗营销型网站建设的目的
  • 国外很炫酷的网站网络工程专业学什么
  • 建设个商城网站需要多少钱关键词筛选
  • 程序可以做网站吗织梦房产网站源码
  • 游戏开发网站开发返利网站建设哪个公司好
  • 网站如何推广行业wordpress扫码枪
  • 机场建设管理投资有限责任公司网站鲁棒导航
  • 免费做网站软件下载网站建设与管理2018
  • 网站 建设 步骤wordpress淘宝客教程
  • WordPress实现评论表情搜索优化师