javascript
springboot主线程_Springboot对多线程的支持详解
Springboot對多線程的支持詳解
這兩天看阿里的JAVA開發(fā)手冊,到多線程的時候說永遠不要用 new Thread()這種方式來使用多線程。確實是這樣的,我一直在用線程池,到了springboot才發(fā)現他已經給我們提供了很方便的線程池機制。
本博客代碼托管在github上https://github.com/gxz0422042...
一、介紹
Spring是通過任務執(zhí)行器(TaskExecutor)來實現多線程和并發(fā)編程,使用ThreadPoolTaskExecutor來創(chuàng)建一個基于線城池的TaskExecutor。在使用線程池的大多數情況下都是異步非阻塞的。我們配置注解@EnableAsync可以開啟異步任務。然后在實際執(zhí)行的方法上配置注解@Async上聲明是異步任務。
二、配置類
配置類代碼如下:
package com.spartajet.springbootlearn.thread;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
* @description
* @create 2017-02-22 下午11:53
* @email gxz04220427@163.com
*/
@Configuration
@EnableAsync
public class ThreadConfig implements AsyncConfigurer {
/**
* The {@link Executor} instance to be used when processing async
* method invocations.
*/
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(15);
executor.setQueueCapacity(25);
executor.initialize();
return executor;
}
/**
* The {@link AsyncUncaughtExceptionHandler} instance to be used
* when an exception is thrown during an asynchronous method execution
* with {@code void} return type.
*/
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
解讀:
利用EnableAsync來開啟Springboot對于異步任務的支持
配置類實現接口AsyncConfigurator,返回一個ThreadPoolTaskExecutor線程池對象。
三、任務執(zhí)行
任務執(zhí)行代碼:
package com.spartajet.springbootlearn.thread;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @description
* @create 2017-02-23 上午12:00
* @email gxz04220427@163.com
*/
@Service
public class AsyncTaskService {
@Async
public void executeAsyncTask(int i) {
System.out.println("線程" + Thread.currentThread().getName() + " 執(zhí)行異步任務:" + i);
}
}
代碼解讀:
通過@Async注解表明該方法是異步方法,如果注解在類上,那表明這個類里面的所有方法都是異步的。
四、測試代碼
package com.spartajet.springbootlearn;
import com.spartajet.springbootlearn.thread.AsyncTaskService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@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);
}
}
}
測試結果:
線程ThreadPoolTaskExecutor-4 執(zhí)行異步任務:3
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務:1
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:0
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:7
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:8
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:9
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:10
線程ThreadPoolTaskExecutor-5 執(zhí)行異步任務:4
線程ThreadPoolTaskExecutor-3 執(zhí)行異步任務:2
線程ThreadPoolTaskExecutor-5 執(zhí)行異步任務:12
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:11
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務:6
線程ThreadPoolTaskExecutor-4 執(zhí)行異步任務:5
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務:16
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:15
線程ThreadPoolTaskExecutor-5 執(zhí)行異步任務:14
線程ThreadPoolTaskExecutor-3 執(zhí)行異步任務:13
線程ThreadPoolTaskExecutor-1 執(zhí)行異步任務:19
線程ThreadPoolTaskExecutor-2 執(zhí)行異步任務:18
線程ThreadPoolTaskExecutor-4 執(zhí)行異步任務:17
總結
以上是生活随笔為你收集整理的springboot主线程_Springboot对多线程的支持详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 游戏加载速度更快,微软 DirectSt
- 下一篇: 因支架不稳定,LG 宣布在北美市场召回