Java默认线程池及线程池参数
⼀、Executors默认创建的线程池
  jdk中Executors提供了⼏种默认的线程池:
1. FixedThreadPool
创建⼀个固定线程数的线程池,核⼼线程数和最⼤线程数固定相等。
keepAliveTime为0,意味着⼀旦有多余的空闲线程,就会被⽴即停⽌掉,不过因为最多只有nThreads个线程,且corePoolSize和maximunPoolSize值⼀致,所以这个值⽆法发挥作⽤。
阻塞队列采⽤了LinkedBlockingQueue,它是⼀个⽆界队列,由于阻塞队列是⼀个⽆界队列,因此永远不可能拒绝任务。
java线程池创建的四种/**
* 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>());
}
2. CachedThreadPool
corePoolSize为0,maximumPoolSize为⽆限⼤,意味着线程数量可以⽆限⼤。
keepAliveTime为60S,意味着线程空闲时间超过60S就会被杀死;
阻塞队列使⽤SynchronousQueue,这个阻塞队列没有存储空间,这意味着只要有请求到来,就必须要到⼀条⼯作线程处理他,如果当前没有空闲的线程,那么就会再创建⼀条新的线程。
* 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>());
}
3. ScheduledThreadPool
创建⼀个调度线程池,即按⼀定的周期执⾏任务,即定时任务
使⽤DelayQueue队列存放等待线程,DelayQueue内部封装了⼀个PriorityQueue,它会根据time的先后时间排序,若time相同则根据sequenceNumber排序;DelayQueue也是⼀个⽆界队列。
/**
* 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);
}
-
-------------------------------------------------------------------------------------------------
/**
* Creates a new {@code ScheduledThreadPoolExecutor} with the
* given core pool size.
*
* @param corePoolSize the number of threads to keep in the pool, even
*        if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @throws IllegalArgumentException if {@code corePoolSize < 0}
*/
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE,
DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
new DelayedWorkQueue());
}
4. newWorkStealingPool
⼀个并⾏的线程池,参数中传⼊的是⼀个线程并发的数量。
封装ForkJoinPool,线程⽆序、抢占式⼯作。
* Creates a work-stealing thread pool using the number of
* {@linkplain Runtime#availableProcessors available processors}
* as its target parallelism level.
*
* @return the newly created thread pool
* @see #newWorkStealingPool(int)
* @since 1.8
*/
public static ExecutorService newWorkStealingPool() {
return new ForkJoinPool
(Runtime().availableProcessors(),
ForkJoinPool.defaultForkJoinWorkerThreadFactory,
null, true);
}
⼆、线程池ThreadPoolExecutor参数
  jdk默认提供的⼏种线程池均由ThreadPoolExecutor创建,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 = (SecurityManager() == null)
null
: Context();
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = Nanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
  这⾥,可以看出ThreadPoolExecutor构造函数有7个参数:
1. corePoolSize
  核⼼线程数。默认情况下,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建⼀个线程去执⾏任务,当线程池中的线程数⽬达到corePoolSize后,就会把到达的任务放到缓存队列当中。核⼼线程在allowCoreThreadTimeout被设置为true时会超时并被回收,默认情况下不会被回收。
2. maximumPoolSize
  最⼤线程数。当线程数⼤于或等于corePoolSize,且任务队列已满时,线程池会创建新的线程,直到线程数量达到
maxPoolSize。如果线程数已等于maxPoolSize,且任务队列已满,则已超出线程池的处理能⼒,线程池会按照
rejectedExecutionHandler配置的处理策略进⾏处理线程。
3. keepAliveTime
  当线程空闲时间达到keepAliveTime,该线程会退出,直到线程数量等于corePoolSize。如果allowCoreThreadTimeout设置为true,则所有线程均会退出直到线程数量为0。
4. TimeUnit
  keepAliveTime的计量单位。
5. workQueue
  阻塞队列,⽤来存储等待执⾏的任务,新任务被提交后,会先进⼊到此⼯作队列中,任务调度时再从队列中取出任务。这⾥的阻塞队列有以下⼏种选择:
1). ArrayBlockingQueue:基于数组的有界阻塞队列,按FIFO排序;
2). LinkedBlockingQueue:基于链表的⽆界阻塞队列(其实最⼤容量为Interger.MAX),按照FIFO排序;
3). SynchronousQueue:⼀个不缓存任务的阻塞队列,也就是说新任务进来时,不会缓存,⽽是直接被调度执⾏该任务;
4). PriorityBlockingQueue:具有优先级的⽆界阻塞队列,优先级通过参数Comparator实现。
6. threadFactory
  线程⼯⼚。创建⼀个新线程时使⽤的⼯⼚,可以⽤来设定线程名、是否为daemon线程等等。
7. RejectedExecutionHandler
  当线程数量达到maxPoolSize时的处理策略,有
1).ThreadPoolExecutor.AbortPolicy: 直接丢弃任务并抛出RejectedExecutionException异常。
2).ThreadPoolExecutor.DiscardPolicy: 直接丢弃任务,但是不抛出异常。
3).ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前⾯的任务,然后重新尝试执⾏任务(重复此过程)
4).ThreadPoolExecutor.CallerRunsPolicy:由调⽤线程处理该任务,在调⽤者线程中直接执⾏被拒绝任务的run⽅法。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。