ArrayBlockingQueue 有界阻塞隊列,此隊列會滿。
SynchronousQueue 沒有容量的阻塞隊列,一次添加必須等待一次獲取。反之亦然。
LinkedBlockingQueue 無界阻塞隊列 這個阻塞隊列用于不會滿
線程池
固定線程數(shù)的線程池
1.核心 線程數(shù) 和 最大線程數(shù)一致。
2.采用的阻塞隊列是 無界阻塞隊列,LinkedBlockingQueue。也就是在極端情況下,阻塞隊列 會一直增長,直到堆內存溢出,需要謹慎使用該線程池。
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);
}
緩沖線程池
0).在newCachedThreadPool中如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。
1).初看該構造函數(shù)時我有這樣的疑惑:核心線程池為0,那按照前面所講的線程池策略新任務來臨時無法進入核心線程池,只能進入 SynchronousQueue中進行等待
,而SynchronousQueue的大小為1,那豈不是第一個任務到達時只能等待在隊列中,直到第二個任務到達發(fā)現(xiàn)無法進入隊列才能創(chuàng)建第一個線程?
2).這個問題的答案在上面講SynchronousQueue時其實已經(jīng)給出了,要將一個元素放入SynchronousQueue中,必須有另一個線程正在等待接收這個元素。因此即便SynchronousQueue一開始為空且大小為1,第一個任務也無法放入其中,因為沒有線程在等待從SynchronousQueue中取走元素。因此第一個任務到達時便會創(chuàng)建一個新線程執(zhí)行該任務。
3).這個最大線程數(shù)是 Integer.MAX 使用中需要注意。
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);
}
newWorkStealingPool
創(chuàng)建一個帶并行級別的線程池,并行級別決定了同一時刻最多有多少個線程在執(zhí)行,如不穿如并行級別參數(shù),將默認為當前系統(tǒng)的CPU個數(shù)
public static ExecutorService newWorkStealingPool(int parallelism) {
//第四個參數(shù)
// asyncMode ? FIFO_QUEUE : LIFO_QUEUE,
return new ForkJoinPool
(parallelism,
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
public static ExecutorService newWorkStealingPool() {
return new ForkJoinPool
(Runtime.getRuntime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
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));
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1, threadFactory));
}
創(chuàng)建一個定長線程池,支持定時及周期性任務執(zhí)行。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public static ScheduledExecutorService newScheduledThreadPool(
int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}