线程池
线程池的实现原理
线程池的使用
(1)线程池的创建
new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,unit,workQueue, handler)
Parameters:
corePoolSize: the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
maximumPoolSize: the maximum number of threads to allow in the pool
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.
unit the time: unit for the keepAliveTime argument
workQueue:the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
threadFactory: set thread factory
handler: the handler to use when execution is blocked because the thread bounds and queue capacities are reached
workQueue:
ArrayBlockingQueue:基于数组结构的有界阻塞队列
LinkedBlockingQueue:基于链表结果的阻塞队列
SynchronousQueue:不存储元素的阻塞队列
PriorityBlockingQueue:具有优先级的无限阻塞队列
handler:
AbortPolicy:直接抛出RejectedExecutionException异常
CallerRunsPolicy:使用调用者所在线程来执行任务
DiscardOldestPolicy:丢弃掉在队列中存在时间最久的任务
DiscardPolicy:默认丢弃任务,不进行任何通知