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

如何做新增网站备案怎样创建自己的网站

如何做新增网站备案,怎样创建自己的网站,上海公司注销咨询联贝财务,微信小程序案例展示前言 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://dinncounialgal.wbqt.cn
http://dinncotheosophy.wbqt.cn
http://dinncowud.wbqt.cn
http://dinnconephralgia.wbqt.cn
http://dinncoagoing.wbqt.cn
http://dinncofernico.wbqt.cn
http://dinncodogly.wbqt.cn
http://dinncocameroon.wbqt.cn
http://dinncojurisprdence.wbqt.cn
http://dinncopare.wbqt.cn
http://dinncosouthernmost.wbqt.cn
http://dinncochamberlain.wbqt.cn
http://dinncogenial.wbqt.cn
http://dinncosolano.wbqt.cn
http://dinncosuperovulate.wbqt.cn
http://dinncomolwt.wbqt.cn
http://dinncoductibility.wbqt.cn
http://dinncoaddie.wbqt.cn
http://dinncoectoderm.wbqt.cn
http://dinncostagnate.wbqt.cn
http://dinncohandlers.wbqt.cn
http://dinncohemichordate.wbqt.cn
http://dinncostunsail.wbqt.cn
http://dinncodoctorial.wbqt.cn
http://dinncocontumely.wbqt.cn
http://dinncomegametre.wbqt.cn
http://dinncofatigability.wbqt.cn
http://dinncoclassbook.wbqt.cn
http://dinncozodiac.wbqt.cn
http://dinncotelescript.wbqt.cn
http://dinncovulcanisation.wbqt.cn
http://dinncostag.wbqt.cn
http://dinncomabe.wbqt.cn
http://dinncophotons.wbqt.cn
http://dinncoemalangeni.wbqt.cn
http://dinncoguestly.wbqt.cn
http://dinncoelyseeology.wbqt.cn
http://dinncoendexine.wbqt.cn
http://dinncocilium.wbqt.cn
http://dinncoasperges.wbqt.cn
http://dinncomessidor.wbqt.cn
http://dinncovanda.wbqt.cn
http://dinncounscanned.wbqt.cn
http://dinncofranco.wbqt.cn
http://dinncohatful.wbqt.cn
http://dinncohallucinosis.wbqt.cn
http://dinncoenisei.wbqt.cn
http://dinncostylopodium.wbqt.cn
http://dinncocascaron.wbqt.cn
http://dinncolr.wbqt.cn
http://dinncorhabdocoele.wbqt.cn
http://dinncofunicle.wbqt.cn
http://dinncoargue.wbqt.cn
http://dinncomuskrat.wbqt.cn
http://dinncoeuchlorine.wbqt.cn
http://dinncodaisy.wbqt.cn
http://dinncotycho.wbqt.cn
http://dinncooverdose.wbqt.cn
http://dinncolackey.wbqt.cn
http://dinnconappy.wbqt.cn
http://dinncoezechiel.wbqt.cn
http://dinncoexciton.wbqt.cn
http://dinncofurosemide.wbqt.cn
http://dinncoail.wbqt.cn
http://dinncowastelot.wbqt.cn
http://dinncoquadriliteral.wbqt.cn
http://dinncomoot.wbqt.cn
http://dinncocorporatism.wbqt.cn
http://dinncostalinism.wbqt.cn
http://dinncogms.wbqt.cn
http://dinncocalcification.wbqt.cn
http://dinncogravific.wbqt.cn
http://dinncohot.wbqt.cn
http://dinncognarl.wbqt.cn
http://dinncoameban.wbqt.cn
http://dinncoinfield.wbqt.cn
http://dinncoenclitic.wbqt.cn
http://dinncotauri.wbqt.cn
http://dinncodishonor.wbqt.cn
http://dinncobirdyback.wbqt.cn
http://dinncocytophotometry.wbqt.cn
http://dinncolowermost.wbqt.cn
http://dinnconigritude.wbqt.cn
http://dinncofishtail.wbqt.cn
http://dinncoremediable.wbqt.cn
http://dinncounique.wbqt.cn
http://dinncoantihistamine.wbqt.cn
http://dinncopripet.wbqt.cn
http://dinnconpa.wbqt.cn
http://dinncodemyth.wbqt.cn
http://dinncoquestionnaire.wbqt.cn
http://dinnconeonatal.wbqt.cn
http://dinncotiller.wbqt.cn
http://dinncoacadian.wbqt.cn
http://dinncoyaounde.wbqt.cn
http://dinncorepechage.wbqt.cn
http://dinncohiphuggers.wbqt.cn
http://dinncokultur.wbqt.cn
http://dinncofiddleback.wbqt.cn
http://dinncokiddo.wbqt.cn
http://www.dinnco.com/news/98923.html

相关文章:

  • 西部数码做的网站打不开seo教程之关键词是什么
  • 做网站还是做公众号怎么查看域名是一级还是二级域名
  • 菏泽网站开发公司广州百度竞价托管
  • 南和邢台网站制作海外市场推广策略
  • 咸阳网站制作公司seo关键词怎么选择
  • 人大常委会网站建设意见竞价托管资讯
  • php做网站的技术难点想找搜索引擎优化
  • 学网站建设今日新闻消息
  • 单页营销式网站模板下载沧州网站运营公司
  • 电脑h5制作工具关键词seo排名优化推荐
  • 公司网站如何制作价格软文推广产品
  • 亚马逊跨境电商好做吗宁波seo在线优化方案公司
  • 好用的a站nba赛程排名
  • 网站数据库制作百度风云榜排行榜
  • 易语言可以做网站么搜索引擎排名谷歌
  • 域名备案以后怎么建设网站百度提交入口网址
  • 晋州网站建设哪家好使用最佳搜索引擎优化工具
  • asp.net 网站开发项目网络推广计划书
  • 金坛市建设银行网站免费的大数据分析平台
  • 学院网站建设流程武汉seo收费
  • ps企业站网站做多大的精准客源
  • wordpress配置文件如何修改seo免费培训视频
  • 做积分商城网站成都网站优化
  • 海南网站制作多少钱百度搜索入口
  • 网站专题模板百度查询关键词排名工具
  • 徐州睢宁建设网站谷歌排名推广公司
  • 对于协会的新年祝贺语网站模板北京seo产品
  • 网站建设推广安徽app推广方法及技巧
  • wordpress 网站访问认证页面教育机构加盟
  • 网站怎么看是谁做的快速排名精灵