您好,登錄后才能下訂單哦!
第一步、配置線程池
package com.kyy.springboot.pool;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* Auth: zhouhongliang
* Date:2019/8/1
*/
@Configuration
public class GlobalConfig {
@Bean
public ThreadPoolTaskExecutor defaultThreadPool(){
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
//核心線程數量
threadPoolTaskExecutor.setCorePoolSize(2);
//最大線程數量
threadPoolTaskExecutor.setMaxPoolSize(5);
//隊列中最大任務數
threadPoolTaskExecutor.setQueueCapacity(2);
//線程名稱前綴
threadPoolTaskExecutor.setThreadNamePrefix("ThreadPool-");
//當達到最大線程數時如何處理新任務
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//線程空閑后最大存活時間
threadPoolTaskExecutor.setKeepAliveSeconds(60);
//初始化線程池
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
}
第二步、創建Service
package com.kyy.springboot.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* Auth: zhouhongliang
* Date:2019/8/1
*/
@Service
public class BootService {
@Resource(name = "defaultThreadPool")
private ThreadPoolTaskExecutor poolTaskExecutor;
@Async
public void testPool() {
System.out.println("線程名稱:" + Thread.currentThread().getName());
}
public void testNoPool() {
System.out.println("線程名稱:" + Thread.currentThread().getName());
}
public int testPoolTaskExecutor(int n) throws InterruptedException, ExecutionException {
CountDownLatch countDownLatch = new CountDownLatch(n);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int sum = 0;
for (int i = 1; i <= n; i++) {
final int index = i;
final Future<Integer> future = poolTaskExecutor.submit(() -> {
Thread.sleep(5000);
System.out.println(simpleDateFormat.format(new Date())+" "+Thread.currentThread().getName() + " 執行 " + index);
countDownLatch.countDown();
return 1;
});
}
countDownLatch.await();
return sum;
}
}
第三步:創建Controller
package com.kyy.springboot.controller;
import com.kyy.springboot.service.BootService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ExecutionException;
/**
* Auth: zhouhongliang
* Date:2019/8/1
*/
@RestController
public class PoolController {
@Autowired
private BootService bootService;
@RequestMapping("/pool")
public String pool(){
for (int i=0;i<100;i++){
bootService.testPool();
}
return "pool test";
}
@RequestMapping("/poolTask/{n}")
public String poolTask(@PathVariable int n){
long startTime = System.currentTimeMillis();
try {
bootService.testPoolTaskExecutor(n);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
return "poolTask test "+(endTime-startTime)/1000+" 秒";
}
}
第四步:創建啟動類
package com.kyy.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* 啟動程序
* @Auther:zhouhongliang
* @Date:2019/7/30
* @Description:
*/
@SpringBootApplication
@EnableAsync
public class SpringBootDemo {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemo.class,args);
}
}
總結:線程池配置說明
1、屬性字段說明
corePoolSize:線程池維護線程的最少數量
keepAliveSeconds:允許的空閑時間
maxPoolSize:線程池維護線程的最大數量
queueCapacity:緩存隊列
rejectedExecutionHandler:對拒絕task的處理策略
2、 execute(Runable)方法執行過程
如果此時線程池中的數量小于corePoolSize,即使線程池中的線程都處于空閑狀態,也要創建新的線程來處理被添加的任務。
如果此時線程池中的數量等于 corePoolSize,但是緩沖隊列 workQueue未滿,那么任務被放入緩沖隊列。
如果此時線程池中的數量大于corePoolSize,緩沖隊列workQueue滿,并且線程池中的數量小于maxPoolSize,建新的線程來處理被添加的任務。
如果此時線程池中的數量大于corePoolSize,緩沖隊列workQueue滿,并且線程池中的數量等于maxPoolSize,那么通過handler所指定的策略來處理此任務。也就是:處理任務的優先級為:核心線程corePoolSize、任務隊列workQueue、最大線程 maximumPoolSize,如果三者都滿了,使用handler處理被拒絕的任務。
當線程池中的線程數量大于corePoolSize時,如果某線程空閑時間超過keepAliveTime,線程將被終止。這樣,線程池可以動態的調整池中的線程數。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。