• spring-boot 多线程




      1 //配置类
      2 
      3 package test;
      4 
      5 import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
      6 import org.springframework.context.annotation.ComponentScan;
      7 import org.springframework.context.annotation.Configuration;
      8 import org.springframework.scheduling.annotation.AsyncConfigurer;
      9 import org.springframework.scheduling.annotation.EnableAsync;
     10 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
     11 
     12 import java.util.concurrent.Executor;
     13 
     14 @Configuration
     15 @ComponentScan("test")
     16 @EnableAsync
     17 public class TaskExecutorConfig implements AsyncConfigurer{
     18 
     19     @Override
     20     public Executor getAsyncExecutor() {
     21         ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
     22         taskExecutor.setCorePoolSize(5);
     23         taskExecutor.setMaxPoolSize(10);
     24         taskExecutor.setQueueCapacity(25);
     25         taskExecutor.initialize();
     26         return taskExecutor;
     27     }
     28 
     29     @Override
     30     public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
     31         return null;
     32     }
     33 }


      1 //任务执行类
      2 
      3 package test;
      4 
      5 import org.springframework.scheduling.annotation.Async;
      6 import org.springframework.stereotype.Service;
      7 
      8 @Service
      9 public class AsyncTaskService {
     10 
     11     @Async
     12     public void executeAsyncTask(Integer i) {
     13         System.out.println("执行异步任务:" + i);
     14     }
     15 }


      1 //运行
      2 
      3 package test;
      4 
      5 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      6 
      7 public class Application {
      8 
      9     public static void main(String[] args) {
     10         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
     11 
     12         AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class);
     13         for (int i = 0; i < 20; i++) {
     14             asyncTaskService.executeAsyncTask(i);
     15         }
     16         context.close();
     17     }
     18 }







    本文出自于:hacpai

  • 相关阅读:
    Python环境变量的配置
    关于selenium+python的googledirver和iedirver的配置
    jdk1.6环境变量配置
    windows server 2012R2安装激活码
    Git生成SSHKey
    Linux下配置和安装VNCServer远程服务
    Win7 64位硬盘安装Ubuntu 64位的细微配置
    apache tomcat 8.0 显示目录文件
    跨域登录
    jsonp 代码优化
  • 原文地址:https://www.cnblogs.com/ios9/p/7473569.html
Copyright © 2020-2023  润新知