Executors封裝好 4 種常見的功能線程池讳苦,如下:
- 定長線程池(FixedThreadPool)
- 定時線程池(ScheduledThreadPool )
- 可緩存線程池(CachedThreadPool)
- 單線程化線程池(SingleThreadExecutor)
定長線程池(FixedThreadPool)
創(chuàng)建方法:
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
特點
只有核心線程气忠,線程數(shù)量固定着饥,執(zhí)行完立即回收,任務隊列為鏈表結構的有界隊列哺徊。
應用場景
控制線程最大并發(fā)數(shù)缺虐。
使用示例
// 1. 創(chuàng)建定長線程池對象 & 設置線程池線程數(shù)量固定為3
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
// 2. 創(chuàng)建好Runnable類線程對象 & 需執(zhí)行的任務
Runnable task =new Runnable(){
public void run() {
System.out.println("執(zhí)行任務啦");
}
};
// 3. 向線程池提交任務
fixedThreadPool.execute(task);
定時線程池(ScheduledThreadPool )
創(chuàng)建方法:
private static final long DEFAULT_KEEPALIVE_MILLIS = 10L;
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE,
DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
new DelayedWorkQueue());
}
public static ScheduledExecutorService newScheduledThreadPool(
int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory) {
super(corePoolSize, Integer.MAX_VALUE,
DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
new DelayedWorkQueue(), threadFactory);
}
特點
核心線程數(shù)量固定,非核心線程數(shù)量無限齐帚,執(zhí)行完閑置 10ms 后回收,任務隊列為延時阻塞隊列彼哼。
應用場景
執(zhí)行定時或周期性的任務对妄。
使用示例
// 1. 創(chuàng)建 定時線程池對象 & 設置線程池線程數(shù)量固定為5
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
// 2. 創(chuàng)建好Runnable類線程對象 & 需執(zhí)行的任務
Runnable task =new Runnable(){
public void run() {
System.out.println("執(zhí)行任務啦");
}
};
// 3. 向線程池提交任務
scheduledThreadPool.schedule(task, 1, TimeUnit.SECONDS); // 延遲1s后執(zhí)行任務
scheduledThreadPool.scheduleAtFixedRate(task,10,1000,TimeUnit.MILLISECONDS);// 延遲10ms后、每隔1000ms執(zhí)行任務
可緩存線程池(CachedThreadPool)
創(chuàng)建方法:
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}
特點
無核心線程敢朱,非核心線程數(shù)量無限饥伊,執(zhí)行完閑置 60s 后回收,任務隊列為不存儲元素的阻塞隊列蔫饰。
應用場景
執(zhí)行大量、耗時少的任務愉豺。
使用示例
// 1. 創(chuàng)建可緩存線程池對象
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
// 2. 創(chuàng)建好Runnable類線程對象 & 需執(zhí)行的任務
Runnable task =new Runnable(){
public void run() {
System.out.println("執(zhí)行任務啦");
}
};
// 3. 向線程池提交任務
cachedThreadPool.execute(task);
單線程化線程池(SingleThreadExecutor)
創(chuàng)建方法:
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory));
}
特點
只有 1 個核心線程篓吁,無非核心線程,執(zhí)行完立即回收蚪拦,任務隊列為鏈表結構的有界隊列杖剪。
應用場景
不適合并發(fā)但可能引起 IO 阻塞性及影響 UI 線程響應的操作,如數(shù)據(jù)庫操作驰贷、文件操作等盛嘿。
使用示例
// 1. 創(chuàng)建單線程化線程池
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
// 2. 創(chuàng)建好Runnable類線程對象 & 需執(zhí)行的任務
Runnable task =new Runnable(){
public void run() {
System.out.println("執(zhí)行任務啦");
}
};
// 3. 向線程池提交任務
singleThreadExecutor.execute(task);
對比
線程池對比