当前位置:   article > 正文

Spring Boot 创建及使用多线程_springboot 创建线程

springboot 创建线程

Spring Boot多线程

1. 介绍

Spring是通过任务执行器(TaskExecutor)来实现多线程和并发编程,使用ThreadPoolTaskExecutor来创建一个基于线城池的TaskExecutor。在使用线程池的大多数情况下都是异步非阻塞的。我们配置注解@EnableAsync可以开启异步任务。然后在实际执行的方法上配置注解@Async上声明是异步任务。

2. 配置类

  1. 首先使用@EnableAsync来开启Springboot对于异步任务的支持

    @SpringBootApplication
    @EnableAsync
    public class SpringBootApplication {
        public static void main(String[] args) {
            SpringApplication.run(SpringBootApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  2. 配置类实现接口AsyncConfigurator,返回一个ThreadPoolTaskExecutor线程池对象。

    @Configuration
    @EnableAsync
    public class AsyncTaskConfig implements AsyncConfigurer {
    
        // ThredPoolTaskExcutor的处理流程
        // 当池子大小小于corePoolSize,就新建线程,并处理请求
        // 当池子大小等于corePoolSize,把请求放入workQueue中,池子里的空闲线程就去workQueue中取任务并处理
        // 当workQueue放不下任务时,就新建线程入池,并处理请求,如果池子大小撑到了maximumPoolSize,就用RejectedExecutionHandler来做拒绝处理
        // 当池子的线程数大于corePoolSize时,多余的线程会等待keepAliveTime长时间,如果无请求可处理就自行销毁
    
        @Override
        @Bean
        public Executor getAsyncExecutor() {
            ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
            //设置核心线程数
            threadPool.setCorePoolSize(10);
            //设置最大线程数
            threadPool.setMaxPoolSize(100);
            //线程池所使用的缓冲队列
            threadPool.setQueueCapacity(10);
            //等待任务在关机时完成--表明等待所有线程执行完
            threadPool.setWaitForTasksToCompleteOnShutdown(true);
            // 等待时间 (默认为0,此时立即停止),并没等待xx秒后强制停止
            threadPool.setAwaitTerminationSeconds(60);
            //  线程名称前缀
            threadPool.setThreadNamePrefix("Derry-Async-");
            // 初始化线程
            threadPool.initialize();
            return threadPool;
        }
    
        @Override
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
            return null;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

3. 基于@Async无返回值调用

3.1 任务执行

通过@Async注解表明该方法是异步方法,如果注解在类上,那表明这个类里面的所有方法都是异步的。

@Service
public class AsyncTaskService {
    @Async
    public void executeAsyncTask(int i) {
        System.out.println("线程" + Thread.currentThread().getName() + " 执行异步任务:" + i);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
3.2 测试代码
@RunWith (SpringRunner.class)
@SpringBootTest
public class SpringbootLearnApplicationTests {
    @Autowired
    private AsyncTaskService asyncTaskService;

    @Test
    public void contextLoads() {
    }

    @Test
    public void threadTest() {
        for (int i = 0; i < 20; i++) {
            asyncTaskService.executeAsyncTask(i);
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

测试结果

线程ThreadPoolTaskExecutor-4 执行异步任务:3
线程ThreadPoolTaskExecutor-2 执行异步任务:1
线程ThreadPoolTaskExecutor-1 执行异步任务:0
线程ThreadPoolTaskExecutor-1 执行异步任务:7
线程ThreadPoolTaskExecutor-1 执行异步任务:8
线程ThreadPoolTaskExecutor-1 执行异步任务:9
线程ThreadPoolTaskExecutor-1 执行异步任务:10
线程ThreadPoolTaskExecutor-5 执行异步任务:4
线程ThreadPoolTaskExecutor-3 执行异步任务:2
线程ThreadPoolTaskExecutor-5 执行异步任务:12
线程ThreadPoolTaskExecutor-1 执行异步任务:11
线程ThreadPoolTaskExecutor-2 执行异步任务:6
线程ThreadPoolTaskExecutor-4 执行异步任务:5
线程ThreadPoolTaskExecutor-2 执行异步任务:16
线程ThreadPoolTaskExecutor-1 执行异步任务:15
线程ThreadPoolTaskExecutor-5 执行异步任务:14
线程ThreadPoolTaskExecutor-3 执行异步任务:13
线程ThreadPoolTaskExecutor-1 执行异步任务:19
线程ThreadPoolTaskExecutor-2 执行异步任务:18
线程ThreadPoolTaskExecutor-4 执行异步任务:17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

4. 基于@Async返回值的调用

通过Future来接受异步方法的处理结果

@Async
	public Future<Long> subByAsync() throws Exception {
		long start = System.currentTimeMillis();
		long sum = 0;
		Thread.sleep(DoTime);
		long end = System.currentTimeMillis();
		sum = end - start;
		logger.info("\t 完成任务一");
		return new AsyncResult<>(sum);
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

返回的数据类型为Future类型,实为一个接口,具体的结果类型为AsyncResult

// 调用异步方法
Future<Long> task = arithmeticService.subByAsync();
// 接受异步方法结果
while (true) {
	if (task.isDone()) {
		long async = task.get();
		logger.info("异步任务执行的时间是:" + async + "(毫秒)");
		break;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Ref

  1. https://cloud.tencent.com/developer/article/1148502
  2. https://github.com/timebusker/spring-boot/tree/master/spring-boot-5-Async
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号