JDK線程池和Spring線程池的使用實(shí)例解析
這篇文章主要介紹了JDK線程池和Spring線程池的使用實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
JDK線程池和Spring線程池實(shí)例,異步調(diào)用,可以直接使用
(1)JDK線程池的使用,此處采用單例的方式提供,見示例:
public class ThreadPoolUtil {
private static int corePoolSize = 5;
private static int maximumPoolSize = 10;
private static long keepAliveTime = 60L;
private static TimeUnit unit = TimeUnit.SECONDS;
private static int capacity = 1024;
private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("jdk-thread-pool-%d").build();
private static final ExecutorService executorService = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize,
keepAliveTime,
unit,
new LinkedBlockingQueue<>(capacity),
namedThreadFactory,
new ThreadPoolExecutor.AbortPolicy());
private ThreadPoolUtil () {
}
public static ExecutorService getExecutorService () {
return executorService;
}
}
在其它地方可以直接這樣使用:
ThreadPoolUtil.getExecutorService().execute(() -> {
System.out.println("test1");
System.out.println("test2");
}
)
(2)Spring線程池的使用,此處通過(guò)配置類的方式配置線程池的相關(guān)屬性,見示例:
@Configuration
@EnableAsync
public class DocataThreadBeanConfig {
private int corePoolSize = 5;
private int maxPoolSize = 10;
private int queueCapacity = 1024;
private String namePrefix = "async-service-task-";
// 上述屬性可以通過(guò)@Value來(lái)讀取配置值
@Bean(name = "asyncServiceTaskExecutor")
public TaskExecutor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 設(shè)置核心線程數(shù)
executor.setCorePoolSize(corePoolSize);
// 設(shè)置最大線程數(shù)
executor.setMaxPoolSize(maxPoolSize);
// 設(shè)置隊(duì)列容量
executor.setQueueCapacity(queueCapacity);
// 設(shè)置線程活躍時(shí)間(秒)
executor.setKeepAliveSeconds(60);
// 設(shè)置默認(rèn)線程名稱
executor.setThreadNamePrefix(namePrefix);
// 設(shè)置拒絕策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任務(wù)結(jié)束后再關(guān)閉線程池
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.initialize();
;
return executor;
}
}
在其它文件中需要這樣使用:
@Resource(name="asyncServiceTaskExecutor") private ThreadPoolTaskExecutor asyncServiceTaskExecutor;
不要直接使用@Autowired,否則會(huì)提示失敗的
@Autowired private ThreadPoolTaskExecutor asyncServiceTaskExecutor;
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 判斷字符串a(chǎn)和b是否互為旋轉(zhuǎn)詞
本篇文章主要介紹了判斷字符串a(chǎn)和b是否互為旋轉(zhuǎn)詞的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05
java讀取cvs文件并導(dǎo)入數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了java讀取cvs文件并導(dǎo)入數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
分享Spring Boot 3.x微服務(wù)升級(jí)歷程
Spring Boot 3.0.0 GA版已經(jīng)發(fā)布,好多人也開始嘗試升級(jí),有人測(cè)試升級(jí)后,啟動(dòng)速度確實(shí)快了不少,這篇文章主要介紹了Spring Boot 3.x微服務(wù)升級(jí)經(jīng)歷,需要的朋友可以參考下2022-12-12
spring cloud服務(wù)之間的調(diào)用之ribbon詳解
關(guān)于spring-cloud的服務(wù)調(diào)用,我們首先需要了解它的兩個(gè)核心組件Ribbon和Feign。接下來(lái)通過(guò)本文給大家詳細(xì)介紹spring-cloud服務(wù)之間的調(diào)用之ribbon,感興趣的朋友一起看看吧2021-08-08
Spring Boot Dubbo 構(gòu)建分布式服務(wù)的方法
這篇文章主要介紹了Spring Boot Dubbo 構(gòu)建分布式服務(wù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05

