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

如何做新增网站备案线上推广方案怎么做

如何做新增网站备案,线上推广方案怎么做,.net 网站地图,网站建设 经营范围前言 Async这个注解想必大家都用过,是用来实现异步调用的。一个方法加上这个注解以后,当被调用时会使用新的线程来调用。但其实这里面也有一个坑。 问题 这个注解使用时存在如下问题:在没有自定义线程池的场景下,默认会采用Sim…

前言

@Async这个注解想必大家都用过,是用来实现异步调用的。一个方法加上这个注解以后,当被调用时会使用新的线程来调用。但其实这里面也有一个坑。

问题

这个注解使用时存在如下问题:在没有自定义线程池的场景下,默认会采用SimpleAsyncTaskExecutor创建线程,线程池的最大大小为Integer的MAX_VALUE,相当于调用一次创建一个线程,缺乏线程重用机制。在并发大的场景下可能引发严重性能问题。下面是他的源代码:

/*** {@link TaskExecutor} implementation that fires up a new Thread for each task,* executing it asynchronously.** <p>Supports limiting concurrent threads through the "concurrencyLimit"* bean property. By default, the number of concurrent threads is unlimited.** <p><b>NOTE: 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时指定线程池。

  1. 使用配置文件中配置默认线程池

    1. application.properties参考配置,yml文件同理。

    2. # 线程池创建时的初始化线程数,默认为8
      spring.task.execution.pool.core-size=1
      # 线程池的最大线`在这里插入代码片`程数,默认为int最大值
      spring.task.execution.pool.max-size=1
      # 用来缓冲执行任务的队列,默认为int最大值
      spring.task.execution.pool.queue-capacity=10
      # 线程终止前允许保持空闲的时间
      spring.task.execution.pool.keep-alive=60s
      # 是否允许核心线程超时
      spring.task.execution.pool.allow-core-thread-timeout=true
      # 是否等待剩余任务完成后才关闭应用
      spring.task.execution.shutdown.await-termination=false
      # 等待剩余任务完成的最大时间
      spring.task.execution.shutdown.await-termination-period=
      # 线程名的前缀,设置好了之后可以方便我们在日志中查看处理任务所在的线程池
      spring.task.execution.thread-name-prefix=asynctask-
      
  2. 通过实现接口配置默认线程池

    1. 实现AsyncConfigurer覆盖getAsyncExecutor()方法。注意:这个方法的优先级比配置文件高

    2. @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;}
      }
      
  3. 单独配置线程池,使用@Async指定线程池

    1. 这种方式可以给每个async的方法指定单独的线程池,但缺点是开发得知道怎么去设置。

    2. /**
      * 独立线程池配置
      */
      @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. 下面是测试代码,大家可以用这个代码分别测试上述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.shuangfujiaoyu.com/news/23625.html

相关文章:

  • 《网站建设 补充合同中国搜索网站排名
  • 网站独立ip如何做301重定向免费软件下载网站有哪些
  • 厂房出租做推广什么网站好百度搜索量怎么查
  • 雄安智能网站建设惠州seo外包服务
  • 那些开店的网站是自己做的吗自动引流推广app
  • python做网站服务器操作系统网站制作步骤流程图
  • 政府网站建设和使用带来哪些积极影响服务营销包括哪些内容
  • 毕业论文网站建设模板公司企业网站建设
  • 网站建设模版品牌营销做得好的品牌有哪些
  • 鞍山市住房和城乡建设网站如何在百度打广告
  • 网站开发工具.枫子科技百度关键字优化价格
  • 网站服务器和空间的区别企业网站seo方案
  • 做网站管理系统网站专业术语中seo意思是
  • 摄影学习网站b站推广形式
  • 艺术字转换器花体字百度推广seo自学
  • 哪些网站可以做付费视频百度收录入口在哪里
  • 自己做网站如何销售下载百度免费版
  • 西安标书制作株洲专业seo优化
  • 网站做产品的审核工作内容百度刷首页怎么刷
  • 网站美工设计收费网站百度权重
  • 人和动物做愛视频网站2345网址导航删除办法
  • crm软件系统 运用seo广告优化
  • 杭州专业做网站的公司有哪些站长seo推广
  • 网站怎么建设与管理单页网站制作教程
  • 延平网站建设wzjseo网络广告营销典型案例
  • 什么网站可以做拍a发bseo算法入门教程
  • 青海网站制作2023年9月疫情又开始了吗
  • 公司的网站都是谁在维护网络营销实训个人总结
  • 建站网页模板免费推广平台有哪些
  • 网站建设网站建设哪里有哪里有整站优化