线程池,是指管理一组同构工作线程的资源池。工作线程的任务是从工作队列中获取一个任务,执行任务,然后返回线程池并等待下一个任务。
1.优点
- 通过重用现有的线程而不是创建新线程,可以避免频繁创建和销毁线程的开销。
- 线程池中的工作线程是创建好的,因此可以避免由于等待创建线程而引起的延迟,提高了响应性。
- 可以适当调整线程池的大小,使处理器保持忙碌状态,也可以防止过多线程相互竞争资源导致资源耗尽。
2.Executors Method Detail
/**
* newFixedThreadPool 创建一个固定长度的线程池,每当提交一个任务时就创建一个线程,
* 直到达到线程的nThreads数量,如果某个线程由于发生Exception而结束,那么线程池会补充一个新的线程。
*/
/**
* 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>());
}
/**
* newSingleThreadExecutor 单线程的Executor,它创建单个worker thread 来执行任务,
* 如果这个线程异常结束,会创建另一个线程来替代。
*/
/**
* 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
* <tt>newFixedThreadPool(1)</tt> 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>()));
}
/**
* newCachedThreadPool 创建一个可缓存的线程池,如果线程池的当前线程数超过处理需求时,
* 那么将回收空闲的线程,当需求增加时,则添加新的线程,线程池的数量不存在限制。
*/
/**
* 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 <tt>execute</tt> 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>());
}
/**
* newScheduledThreadPool 创建一个固定长度的线程池,而且以延迟或定时的方法来执行任务。
* 类似于Timer
*/
/**
* 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);
}