說明
- 線程池是多線程的處理機制,線程池一般用于需要大量線程完成任務(wù)恭取,并且完成時間較短時使用泰偿,大量用于并發(fā)框架和異步執(zhí)行任務(wù)。
優(yōu)點
- 降低資源消耗秽荤,通過利用已創(chuàng)建的線程降低線程創(chuàng)建和銷毀造成的消耗
- 有利于線程的可控性,如果線程無休止創(chuàng)建柠横,會導(dǎo)致內(nèi)存耗盡窃款。
- 提高系統(tǒng)響應(yīng)速度,通過使用已存在的線程牍氛,不需要等待新線程的創(chuàng)建就可以立即執(zhí)行當(dāng)前任務(wù)晨继。
主要參數(shù)簡單解釋
- corePoolSize:核心線程數(shù),默認(rèn)的核心線程的1搬俊,向線程池提交一個任務(wù)時紊扬,如果線程池已經(jīng)創(chuàng)建的線程數(shù)小于核心線程數(shù),即使此時存在空閑線程唉擂,也會通過創(chuàng)建一個新線程來執(zhí)行新任務(wù)餐屎,知道創(chuàng)建的線程等于核心線程數(shù)時,如果有空閑線程玩祟,則使用空閑線程腹缩。
- maxPoolSize:最大線程數(shù),默認(rèn)的最大線程數(shù)為Integer.MAX_VALUE 即231-1空扎。當(dāng)隊列滿了之后
- keepAliveSeconds:允許線程空閑時間藏鹊,默認(rèn)的線程空閑時間為60秒,當(dāng)線程中的線程數(shù)大于核心線程數(shù)時转锈,線程的空閑時間如果超過線程的存活時間盘寡,則此線程會被銷毀,直到線程池中的線程數(shù)小于等于核心線程數(shù)時撮慨。
- queueCapacity:緩沖隊列數(shù)竿痰,默認(rèn)的緩沖隊列數(shù)是Integer.MAX_VALUE 即231-1,用于保存執(zhí)行任務(wù)的阻塞隊列
- allowCoreThreadTimeOut:銷毀機制砌溺,allowCoreThreadTimeOut為true則線程池數(shù)量最后銷毀到0個菇曲。allowCoreThreadTimeOut為false銷毀機制:超過核心線程數(shù)時,而且(超過最大值或者timeout過)抚吠,就會銷毀常潮。默認(rèn)是false
完整代碼地址在結(jié)尾!楷力!
第一步喊式,配置application.yml孵户,避免端口沖突
# 配置線程池
threadPoolTaskExecutor:
corePoolSize: 10 # 核心線程數(shù)(默認(rèn)線程數(shù))
maxPoolSize: 100 # 最大線程數(shù)
keepAliveTime: 10 # 允許線程空閑時間(單位:默認(rèn)為秒)
queueCapacity: 200 # 緩沖隊列數(shù)
threadNamePrefix: custom-executor- # 線程名統(tǒng)一前綴
server:
port: 8099
spring:
application:
name: threadpool-demo-server
第二步,創(chuàng)建ThreadPoolTaskExecutorConfig配置類岔留,如下
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @version 1.0
* @author luoyu
* @date 2019-08-09
* @description 線程池配置
*/
@Configuration
@EnableAsync
@EnableScheduling
public class ThreadPoolTaskExecutorConfig {
/**
* 核心線程數(shù)(默認(rèn)線程數(shù))
*/
@Value("${threadPoolTaskExecutor.corePoolSize}")
private int corePoolSize;
/**
* 最大線程數(shù)
*/
@Value("${threadPoolTaskExecutor.maxPoolSize}")
private int maxPoolSize;
/**
* 允許線程空閑時間(單位:默認(rèn)為秒)
*/
@Value("${threadPoolTaskExecutor.keepAliveTime}")
private int keepAliveTime;
/**
* 緩沖隊列數(shù)
*/
@Value("${threadPoolTaskExecutor.queueCapacity}")
private int queueCapacity;
/**
* 線程池名前綴
*/
@Value("${threadPoolTaskExecutor.threadNamePrefix}")
private String threadNamePrefix;
/**
* @return ThreadPoolTaskExecutor
* @author jinhaoxun
* @description 線程池配置夏哭,bean的名稱,默認(rèn)為首字母小寫的方法名taskExecutor
*/
@Bean("testTaskExecutor")
public ThreadPoolTaskExecutor taskExecutor1() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//設(shè)置核心線程數(shù)
executor.setCorePoolSize(corePoolSize);
//設(shè)置最大線程數(shù)
executor.setMaxPoolSize(maxPoolSize);
//線程池所使用的緩沖隊列
executor.setQueueCapacity(queueCapacity);
//等待任務(wù)在關(guān)機時完成--表明等待所有線程執(zhí)行完
executor.setWaitForTasksToCompleteOnShutdown(true);
// 等待時間 (默認(rèn)為0献联,此時立即停止)竖配,并沒等待xx秒后強制停止
executor.setKeepAliveSeconds(keepAliveTime);
// 線程名稱前綴
executor.setThreadNamePrefix(threadNamePrefix);
// 線程池對拒絕任務(wù)的處理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
executor.initialize();
return executor;
}
}
說明
- @EnableAsync開啟@Async注解支持,也可以添加在啟動類上
- @EnableScheduling開啟@Scheduled注解支持里逆,可以使用線程池配置定時任務(wù)进胯,也可以添加在啟動類上
第三步,創(chuàng)建類服務(wù)類原押,TestService胁镐,TestServiceImpl,如下
TestService
public interface TestService {
void test1();
void test2();
void test3();
void test4();
}
TestServiceImpl
import com.luoyu.threadpool.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Slf4j
@Service
public class TestServiceImpl implements TestService {
@Resource(name = "testTaskExecutor")
private ThreadPoolTaskExecutor testTaskExecutor;
// 定時任務(wù)诸衔,一秒執(zhí)行一次
@Scheduled(fixedRate = 1000)
@Override
public void test1() {
log.info("定時任務(wù)盯漂,一秒執(zhí)行一次");
}
@Override
public void test2() {
log.info("看看是哪個線程執(zhí)行了我!");
}
@Override
public void test3() {
testTaskExecutor.execute(() -> {
log.info("看看是哪個線程執(zhí)行了我笨农!");
});
}
@Async("testTaskExecutor")
@Override
public void test4() {
log.info("看看是哪個線程執(zhí)行了我就缆!");
}
}
第四步,創(chuàng)建類單元測試類谒亦,ThreadpoolApplicationTests违崇,并進行測試,如下
import com.luoyu.threadpool.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@Slf4j
// 獲取啟動類诊霹,加載配置羞延,確定裝載 Spring 程序的裝載方法,它回去尋找 主配置啟動類(被 @SpringBootApplication 注解的)
@SpringBootTest
class ThreadpoolApplicationTests {
@Autowired
private TestService testService;
@Test
void test2(){
testService.test2();
}
@Test
void test3(){
testService.test3();
}
@Test
void test4(){
testService.test4();
}
@BeforeEach
void testBefore(){
log.info("測試開始!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
@AfterEach
void testAfter(){
log.info("測試結(jié)束!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
第五步脾还,測試定時任務(wù)的話伴箩,只需要啟動項目,查看控制臺日志即可
注意鄙漏,@Async注解失效可能原因
- 沒有在@SpringBootApplication啟動類當(dāng)中添加注解@EnableAsync注解
- 異步方法使用注解@Async的返回值只能為void或者Future
- 沒有走Spring的代理類嗤谚。因為@Transactional和@Async注解的實現(xiàn)都是基于Spring的AOP,而AOP的實現(xiàn)是基于動態(tài)代理模式實現(xiàn)的怔蚌。那么注解失效的原因就很明顯了巩步,有可能因為調(diào)用方法的是對象本身而不是代理對象,因為沒有經(jīng)過Spring容器
第六步桦踊,獲取線程池中線程的返回結(jié)果椅野,修改TestService,TestServiceImpl新增方法,如下
TestService
public interface TestService {
void test1();
void test2();
void test3();
void test4();
void test5() throws Exception;
}
TestServiceImpl
import com.luoyu.threadpool.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.Future;
@Slf4j
@Service
public class TestServiceImpl implements TestService {
@Resource(name = "testTaskExecutor")
private ThreadPoolTaskExecutor testTaskExecutor;
// 定時任務(wù)竟闪,一秒執(zhí)行一次
@Scheduled(fixedRate = 1000)
@Override
public void test1() {
log.info("定時任務(wù)离福,一秒執(zhí)行一次,看看是哪個線程執(zhí)行了我炼蛤!{}", Thread.currentThread().getName());
}
@Override
public void test2() {
log.info("看看是哪個線程執(zhí)行了我妖爷!{}", Thread.currentThread().getName());
}
@Override
public void test3() {
for (int i = 0; i < 10; i++) {
testTaskExecutor.execute(() -> {
log.info("看看是哪個線程執(zhí)行了我!{}", Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
}
@Async("testTaskExecutor")
@Override
public void test4() {
log.info("看看是哪個線程執(zhí)行了我理朋!{}", Thread.currentThread().getName());
}
@Override
public void test5() throws Exception {
// 啟動兩個線程執(zhí)行子任務(wù)
Future<Integer> count1 = testTaskExecutor.submit(() -> this.getCount1());
Future<Integer> count2 = testTaskExecutor.submit(() -> this.getCount2());
// 此處主線程進行阻塞
Integer integer1 = count1.get();
Integer integer2 = count2.get();
// 拿到子線程返回結(jié)果
log.info("1:" + integer1 + "絮识,2:" + integer2);
}
private Integer getCount1() throws InterruptedException {
Thread.sleep(5000);
return 50;
}
private Integer getCount2() throws InterruptedException {
Thread.sleep(3000);
return 30;
}
}
第七步,修改單元測試類嗽上,ThreadpoolApplicationTests次舌,新增測試方法并進行測試,如下
import com.luoyu.threadpool.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@Slf4j
// 獲取啟動類炸裆,加載配置垃它,確定裝載 Spring 程序的裝載方法鲜屏,它回去尋找 主配置啟動類(被 @SpringBootApplication 注解的)
@SpringBootTest
class ThreadpoolApplicationTests {
@Autowired
private TestService testService;
@Test
void test2(){
testService.test2();
}
@Test
void test3(){
testService.test3();
}
@Test
void test4(){
testService.test4();
}
@Test
void test5() throws Exception {
testService.test5();
}
@BeforeEach
void testBefore(){
log.info("測試開始!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
@AfterEach
void testAfter(){
log.info("測試結(jié)束!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
注:此工程包含多個module烹看,本文所用代碼均在threadpool-demo模塊下
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者