线程池的创建(2种)
为何要创建线程池
线程池是一种池化技术,目的是避免线程频繁的创建和销毁带来的性能消耗。它是把已创建的线程放入“池”中,当有任务来临时就可以重用已有的线程,无需等待创建的过程,这样就可以有效提高程序的响应速度。
《Java 并发编程艺术》一书中提到使用线程池的好处:
降低资源消耗。通过重复利用已创建的线程降低线程创建和销毁造成的资源浪费。
提高响应速度。当任务到达时,不需要等到线程创建就能立即执行。
方便管理线程。线程是稀缺资源,如果无限制地创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以对线程进行统一的分配,优化及监控。
1、JDK自带的线程池工具类 Executors
Executors类实际上是对ThreadPoolExecutor的封装,隐藏了创建线程池需要的诸多参数,可以直接调用静态方法快速创建线程池,但这样同时也存在弊端,就是对新手不太友好,隐藏了构建参数无法清晰的看出线程池内部的运行规则,存在OOM的风险。
如下可以看到Executors创建线程池的静态方法
下面结合源码和示例体会一下
①实例中线程池中固定线程数量2,同时添加6个任务,最多也只有2个线程在工作,其余的在阻塞队列中排队等待
创建一个线程池,该线程池重用在共享无界队列上运行的固定数量的线程。在任何时候,最多nThreads线程将是活动的处理任务。如果在所有线程都处于活动状态时提交了其他任务,它们将在队列中等待,直到有线程可用。如果任何线程在关闭之前的执行过程中由于失败而终止,如果需要执行后续任务,新的线程将取代它。池中的线程将一直存在,直到显式shutdown 。
参数:
nThreads – 池中的线程数
回报:
新创建的线程池
抛出:
IllegalArgumentException – 如果nThreads <= 0
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
②单线程线程池,只有一个线程的线程池,因此所有提交的任务是顺序执行
/**
* 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>()));
}
③缓存线程池,线程池里有很多线程需要同时执行,老的可用线程将被新的任务触发重新执行,如果线程超过60秒内没执行,那么将被终止并从池中删除,图中6个任务则创建了6个线程。
/**
* 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>());
}
④周期性线程池,用来调度即将执行的任务的线程池
/**
* 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);
}
2、ThreadPoolExecutor
/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
核心参数:
corePoolSize:核心线程数,空闲时线程池中存在的线程数量,默认allowCoreThreadTimeOut为false
注:线程池设置,allowCoreThreadTimeOut(Boolean boolean),缺省值为FALSE表示核心线程空闲时间超过最大空闲时间不回收,若设置为TRUE则会回收,线程池空闲时最终线程数量为0
maximumPoolSize:最大线程数,线程池中允许存在的最大线程数量
keepAliveTime:当线程数大于核心时,这是多余的空闲线程在终止前等待新任务的最长时间
unit:keepAliveTime参数的时间单位
workQueue:是用于在执行任务之前保存任务的队列,此队列将仅保存由execute方法提交的Runnable任务
threadFactory:执行器创建新线程时使用的工厂
handler:由于达到线程边界和队列容量而阻塞执行时使用的处理程序
常见使用多线程的场景,批量处理数据时,生产或者消费消息时
实际工作中使用多线程最好遵守阿里巴巴java开发手册
所以最建议的创建线程的方式就是new ThreadPoolExecutor。