在更大的线程池中,如果所有正在执行任务的线程都由于等待其他仍处于工作队列中的任务而阻塞,这种现象叫线程饥饿死锁(Thread Starvation DeadLock)
线程池的使用
/**
* Creates a new <tt>ThreadPoolExecutor</tt> with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the
* pool, even if they are idle.
* @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 keepAliveTime
* argument.
* @param workQueue the queue to use for holding tasks before they
* are executed. This queue will hold only the <tt>Runnable</tt>
* tasks submitted by the <tt>execute</tt> 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 corePoolSize or
* keepAliveTime less than zero, or if maximumPoolSize less than or
* equal to zero, or if corePoolSize greater than maximumPoolSize.
* @throws NullPointerException if <tt>workQueue</tt>
* or <tt>threadFactory</tt> or <tt>handler</tt> are 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.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
参数配置:
corepoolSize 为核心池大小
maximumpoolSize最大线程池大小
keepAliveTime 当前线程大小 大于核心池大小时,线程空闲的时间超过keepAliveTime,则终止该线程
unit 为keepAliveTime 的单位
workQueue 为工作队列
threadFactory 创建线程的方式
handler 饱和策略
ThreadPoolExecutor 运作机制详解:
a . 当线程池大小小于 corepoolSize ,并处理请求
b .当线程池大小等于corepoolSize ,把请求放入workQueue中,池子里的空闲线程就去从workQueue中取任务并处理
c .当workQueue放不下新入的任务时,新建线程入池,并处理请求,如果池子大小撑到maximumpoolSize就用RejectedExecutionHandler来做拒绝处理
d .当线程池的线程数大于corePoolSize的时候,多余的线程会等待KeepAliveTime长的时间,如果无请求就自行销毁。
线程池的使用
/**
* Creates a new <tt>ThreadPoolExecutor</tt> with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the
* pool, even if they are idle.
* @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 keepAliveTime
* argument.
* @param workQueue the queue to use for holding tasks before they
* are executed. This queue will hold only the <tt>Runnable</tt>
* tasks submitted by the <tt>execute</tt> 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 corePoolSize or
* keepAliveTime less than zero, or if maximumPoolSize less than or
* equal to zero, or if corePoolSize greater than maximumPoolSize.
* @throws NullPointerException if <tt>workQueue</tt>
* or <tt>threadFactory</tt> or <tt>handler</tt> are 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.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
参数配置:
corepoolSize 为核心池大小
maximumpoolSize最大线程池大小
keepAliveTime 当前线程大小 大于核心池大小时,线程空闲的时间超过keepAliveTime,则终止该线程
unit 为keepAliveTime 的单位
workQueue 为工作队列
threadFactory 创建线程的方式
handler 饱和策略
ThreadPoolExecutor 运作机制详解:
a . 当线程池大小小于 corepoolSize ,并处理请求
b .当线程池大小等于corepoolSize ,把请求放入workQueue中,池子里的空闲线程就去从workQueue中取任务并处理
c .当workQueue放不下新入的任务时,新建线程入池,并处理请求,如果池子大小撑到maximumpoolSize就用RejectedExecutionHandler来做拒绝处理
d .当线程池的线程数大于corePoolSize的时候,多余的线程会等待KeepAliveTime长的时间,如果无请求就自行销毁。