JDK線程池和Spring線程池的使用實例解析
這篇文章主要介紹了JDK線程池和Spring線程池的使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
JDK線程池和Spring線程池實例,異步調(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ān)屬性,見示例:
@Configuration @EnableAsync public class DocataThreadBeanConfig { private int corePoolSize = 5; private int maxPoolSize = 10; private int queueCapacity = 1024; private String namePrefix = "async-service-task-"; // 上述屬性可以通過@Value來讀取配置值 @Bean(name = "asyncServiceTaskExecutor") public TaskExecutor asyncServiceExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); // 設(shè)置核心線程數(shù) executor.setCorePoolSize(corePoolSize); // 設(shè)置最大線程數(shù) executor.setMaxPoolSize(maxPoolSize); // 設(shè)置隊列容量 executor.setQueueCapacity(queueCapacity); // 設(shè)置線程活躍時間(秒) executor.setKeepAliveSeconds(60); // 設(shè)置默認線程名稱 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,否則會提示失敗的
@Autowired private ThreadPoolTaskExecutor asyncServiceTaskExecutor;
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中的線程同步與ThreadLocal無鎖化線程封閉實現(xiàn)
這篇文章主要介紹了Java中的線程同步與ThreadLocal無鎖化線程封閉實現(xiàn),Synchronized關(guān)鍵字與ThreadLocal變量的使用是Java中線程控制的基礎(chǔ),需要的朋友可以參考下2016-03-03關(guān)于Springboot+gateway整合依賴并處理依賴沖突問題
這篇文章主要介紹了Springboot+gateway整合依賴并處理依賴沖突問題,給大家提到了spring boot版本和spring cloud版本,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01springboot使用log4j2異步日志提升性能的實現(xiàn)方式
這篇文章主要介紹了springboot使用log4j2異步日志提升性能,異步日志實現(xiàn)方式:將日志存入一個單獨的隊列中,有一個單獨的線程從隊列中獲取日志并寫入磁盤文件,需要的朋友可以參考下2022-05-05基于javamelody監(jiān)控springboot項目過程詳解
這篇文章主要介紹了基于javamelody監(jiān)控springboot項目過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11JVM性能調(diào)優(yōu)實戰(zhàn):讓你的IntelliJ Idea縱享絲滑
這篇文章主要介紹了JVM性能調(diào)優(yōu)實戰(zhàn):讓你的IntelliJ Idea縱享絲滑的相關(guān)資料,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01