java創(chuàng)建線程池的7種實(shí)現(xiàn)方法
這 7 種實(shí)現(xiàn)方法分別是:
- Executors.newFixedThreadPool:創(chuàng)建一個(gè)固定大小的線程池,可控制并發(fā)的線程數(shù),超出的線程會(huì)在隊(duì)列中等待。
- Executors.newCachedThreadPool:創(chuàng)建一個(gè)可緩存的線程池,若線程數(shù)超過(guò)處理所需,緩存一段時(shí)間后會(huì)回收,若線程數(shù)不夠,則新建線程。
- Executors.newSingleThreadExecutor:創(chuàng)建單個(gè)線程數(shù)的線程池,它可以保證先進(jìn)先出的執(zhí)行順序。
- Executors.newScheduledThreadPool:創(chuàng)建一個(gè)可以執(zhí)行延遲任務(wù)的線程池。
- Executors.newSingleThreadScheduledExecutor:創(chuàng)建一個(gè)單線程的可以執(zhí)行延遲任務(wù)的線程池。
- Executors.newWorkStealingPool:創(chuàng)建一個(gè)搶占式執(zhí)行的線程池(任務(wù)執(zhí)行順序不確定)【JDK 1.8 添加】。
- ThreadPoolExecutor:手動(dòng)創(chuàng)建線程池的方式,它創(chuàng)建時(shí)最多可以設(shè)置 7 個(gè)參數(shù)。
一、用ExecutorService創(chuàng)建線程池
//1.創(chuàng)建一個(gè)大小為10的線程池
ExecutorService threadPool= Executors.newFixedThreadPool(10);
//給線程池添加任務(wù)
for (int i = 0;i < 10;i++){
threadPool.submit(new Runnable() {
@Override
public synchronized void run(){
//這里寫(xiě)你的方法
log.info("開(kāi)啟線程..");
}
});
}二、用ThreadPoolExecutor創(chuàng)建線程池
//1.創(chuàng)建一個(gè)大小為10的線程池
BlockingQueue queue = new LinkedBlockingQueue();
ThreadPoolExecutor executor= new ThreadPoolExecutor(10,Integer.MAX_VALUE,10L, TimeUnit.SECONDS,queue);
//給線程池添加任務(wù)
for (int i = 0;i < 10;i++){
executor.execute(new Runnable() {
@Override
public synchronized void run(){
//這里寫(xiě)你的方法
log.info("開(kāi)啟線程..");
}
});
}從根本上說(shuō)ExecutorService算是通過(guò)ThreadPoolExecutor封裝出來(lái)的他們的底層其實(shí)是一樣的
ThreadPoolExecutor需要傳參,而ExecutorService有默認(rèn)值,值需要一個(gè)線程數(shù)量就可以了。
三、用ThreadPoolUtils工具類(lèi)創(chuàng)建線程池
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.*;
/**
* 自定義線程創(chuàng)建工具類(lèi),創(chuàng)建線程池后不需要關(guān)閉
*
* @author liangxn
*/
public class ThreadPoolUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(ThreadPoolUtils.class);
private static ThreadPoolExecutor threadPool = null;
private static final String POOL_NAME = "myPool";
// 等待隊(duì)列長(zhǎng)度
private static final int BLOCKING_QUEUE_LENGTH = 1000;
// 閑置線程存活時(shí)間
private static final int KEEP_ALIVE_TIME = 5 * 1000;
private ThreadPoolUtils() {
throw new IllegalStateException("utility class");
}
/**
* 無(wú)返回值直接執(zhí)行
*
* @param runnable 需要運(yùn)行的任務(wù)
*/
public static void execute(Runnable runnable) {
getThreadPool().execute(runnable);
}
/**
* 有返回值執(zhí)行
* 主線程中使用Future.get()獲取返回值時(shí),會(huì)阻塞主線程,直到任務(wù)執(zhí)行完畢
*
* @param callable 需要運(yùn)行的任務(wù)
*/
public static <T> Future<T> submit(Callable<T> callable) {
return getThreadPool().submit(callable);
}
/**
* 停止線程池所有任務(wù)
* @return
*/
public static synchronized void shutdownNow() {
getThreadPool().shutdownNow();
}
/**
* 獲取線程池中活躍線程數(shù)
* @return
*/
public static int getActiveCount() {
return getThreadPool().getActiveCount();
}
private static synchronized ThreadPoolExecutor getThreadPool() {
if (threadPool == null) {
// 獲取處理器數(shù)量
int cpuNum = Runtime.getRuntime().availableProcessors();
// 根據(jù)cpu數(shù)量,計(jì)算出合理的線程并發(fā)數(shù)
int maximumPoolSize = cpuNum * 2 + 1;
// 核心線程數(shù)、最大線程數(shù)、閑置線程存活時(shí)間、時(shí)間單位、線程隊(duì)列、線程工廠、當(dāng)前線程數(shù)已經(jīng)超過(guò)最大線程數(shù)時(shí)的異常處理策略
threadPool = new ThreadPoolExecutor(maximumPoolSize - 1,
maximumPoolSize,
KEEP_ALIVE_TIME,
TimeUnit.MILLISECONDS,
new LinkedBlockingDeque<>(BLOCKING_QUEUE_LENGTH),
new ThreadFactoryBuilder().setNameFormat(POOL_NAME + "-%d").build(),
new ThreadPoolExecutor.AbortPolicy() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
LOGGER.warn("線程爆炸了,當(dāng)前運(yùn)行線程總數(shù):{},活動(dòng)線程數(shù):{}。等待隊(duì)列已滿(mǎn),等待運(yùn)行任務(wù)數(shù):{}",
e.getPoolSize(),
e.getActiveCount(),
e.getQueue().size());
}
});
}
return threadPool;
}
}總結(jié)
到此這篇關(guān)于java創(chuàng)建線程池的7種實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)java創(chuàng)建線程池內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用zookeeper實(shí)現(xiàn)的分布式鎖示例
這篇文章主要介紹了java使用zookeeper實(shí)現(xiàn)的分布式鎖示例,需要的朋友可以參考下2014-05-05
java 判斷一個(gè)數(shù)是否為2的整數(shù)次冪方法
今天小編就為大家分享一篇java 判斷一個(gè)數(shù)是否為2的整數(shù)次冪方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Java多種經(jīng)典排序算法(含動(dòng)態(tài)圖)
排序算法是老生常談的了,但是在面試中也有會(huì)被問(wèn)到,例如有時(shí)候,在考察算法能力的時(shí)候,不讓你寫(xiě)算法,就讓你描述一下,某個(gè)排序算法的思想以及時(shí)間復(fù)雜度或空間復(fù)雜度。我就遇到過(guò),直接問(wèn)快排的,所以這次我就總結(jié)梳理一下經(jīng)典的十大排序算法以及它們的模板代碼2021-04-04
基于jdk動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理實(shí)現(xiàn)及區(qū)別說(shuō)明
這篇文章主要介紹了基于jdk動(dòng)態(tài)代理和cglib動(dòng)態(tài)代理實(shí)現(xiàn)及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
解決mybatis plus 分頁(yè)查詢(xún)有條數(shù),total和pages都是零的問(wèn)題
這篇文章主要介紹了解決mybatis plus 分頁(yè)查詢(xún)有條數(shù),total和pages都是零的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11

