一、newCachedThreadPool
創(chuàng)建一個(gè)可緩存線程池陆赋,如果線程池長(zhǎng)度超過(guò)處理需要,可靈活回收空閑線程嚷闭,若無(wú)可回收攒岛,則新建線程。
/**
* Creates a thread pool that creates new threads as needed, but
* will reuse previously constructed threads when they are
* available. These pools will typically improve the performance
* of programs that execute many short-lived asynchronous tasks.
* Calls to {@code execute} will reuse previously constructed
* threads if available. If no existing thread is available, a new
* thread will be created and added to the pool. Threads that have
* not been used for sixty seconds are terminated and removed from
* the cache. Thus, a pool that remains idle for long enough will
* not consume any resources. Note that pools with similar
* properties but different details (for example, timeout parameters)
* may be created using {@link ThreadPoolExecutor} constructors.
*
* @return the newly created thread pool
*/
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
- corePoolSize = 0
- maximumPoolSize = Integer.MAX_VALUE
SynchronousQueue是一個(gè)只有1個(gè)元素的隊(duì)列胞锰,入隊(duì)的任務(wù)需要一直等待直到隊(duì)列中的元素被移出灾锯。核心線程數(shù)是0,意味著所有任務(wù)會(huì)先入隊(duì)列嗅榕;最大線程數(shù)是Integer.MAX_VALUE顺饮,可以認(rèn)為線程數(shù)量是沒(méi)有限制的。KeepAlive時(shí)間被設(shè)置成60秒凌那,意味著在沒(méi)有任務(wù)的時(shí)候線程等待60秒以后退出兼雄。CachedThreadPool對(duì)任務(wù)的處理策略是提交的任務(wù)會(huì)立即分配一個(gè)線程進(jìn)行執(zhí)行,線程池中線程數(shù)量會(huì)隨著任務(wù)數(shù)的變化自動(dòng)擴(kuò)張和縮減帽蝶,在任務(wù)執(zhí)行時(shí)間無(wú)限延長(zhǎng)的極端情況下會(huì)創(chuàng)建過(guò)多的線程赦肋。
二、newFixedThreadPool
創(chuàng)建一個(gè)定長(zhǎng)線程池励稳,可控制線程最大并發(fā)數(shù)佃乘,超出的線程會(huì)在隊(duì)列中等待。
/**
* Creates a thread pool that reuses a fixed number of threads
* operating off a shared unbounded queue. At any point, at most
* {@code nThreads} threads will be active processing tasks.
* If additional tasks are submitted when all threads are active,
* they will wait in the queue until a thread is available.
* If any thread terminates due to a failure during execution
* prior to shutdown, a new one will take its place if needed to
* execute subsequent tasks. The threads in the pool will exist
* until it is explicitly {@link ExecutorService#shutdown shutdown}.
*
* @param nThreads the number of threads in the pool
* @return the newly created thread pool
* @throws IllegalArgumentException if {@code nThreads <= 0}
*/
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
- corePoolSize == maximumPoolSize
- new LinkedBlockingQueue<Runnable>() 【 DefaltCapacity = Integer.MAX_VALUE】
FixedThreadPool的優(yōu)點(diǎn)是能夠保證所有的任務(wù)都被執(zhí)行驹尼,永遠(yuǎn)不會(huì)拒絕新的任務(wù)趣避;同時(shí)缺點(diǎn)是隊(duì)列數(shù)量沒(méi)有限制,在任務(wù)執(zhí)行時(shí)間無(wú)限延長(zhǎng)的這種極端情況下會(huì)造成內(nèi)存問(wèn)題新翎。
三程帕、newSingleThreadExecutor
創(chuàng)建一個(gè)單線程化的線程池,它只會(huì)用唯一的工作線程來(lái)執(zhí)行任務(wù)地啰,保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級(jí))執(zhí)行骆捧。
/**
* Creates an Executor that uses a single worker thread operating
* off an unbounded queue. (Note however that if this single
* thread terminates due to a failure during execution prior to
* shutdown, a new one will take its place if needed to execute
* subsequent tasks.) Tasks are guaranteed to execute
* sequentially, and no more than one task will be active at any
* given time. Unlike the otherwise equivalent
* {@code newFixedThreadPool(1)} the returned executor is
* guaranteed not to be reconfigurable to use additional threads.
*
* @return the newly created single-threaded Executor
*/
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
- corePoolSize = 1
- maximumPoolSize = 1
- new LinkedBlockingQueue<Runnable>() 【 DefaltCapacity = Integer.MAX_VALUE】
這個(gè)工廠方法中使用無(wú)界LinkedBlockingQueue,并的將線程數(shù)設(shè)置成1髓绽,除此以外還使用FinalizableDelegatedExecutorService類進(jìn)行了包裝敛苇。這個(gè)包裝類的主要目的是為了屏蔽ThreadPoolExecutor中動(dòng)態(tài)修改線程數(shù)量的功能,僅保留ExecutorService中提供的方法。雖然是單線程處理枫攀,一旦線程因?yàn)樘幚懋惓5仍蚪K止的時(shí)候括饶,ThreadPoolExecutor會(huì)自動(dòng)創(chuàng)建一個(gè)新的線程繼續(xù)進(jìn)行工作。
四来涨、newScheduledThreadPool
創(chuàng)建一個(gè)定長(zhǎng)線程池图焰,支持定時(shí)及周期性任務(wù)執(zhí)行。
/**
* Creates a thread pool that can schedule commands to run after a
* given delay, or to execute periodically.
* @param corePoolSize the number of threads to keep in the pool,
* even if they are idle
* @return a newly created scheduled thread pool
* @throws IllegalArgumentException if {@code corePoolSize < 0}
*/
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}