ThreadPool
2022-03-30 16:17:23 2 举报
线程池源码分析
作者其他创作
大纲/内容
拒绝策略:CallerRunsPolicy - 返回给调用者AbortPolicy - 抛异常DiscardPolicy - 直接忽略DiscardOldestPolicy - 丢弃老的
LinkedBlockingQueue.take()
falsecore = false
execute
workerCountOf < corePoolSize
mainLock
2、大于核心数:task-offer
3、核心线程数、队列满了、且工作线程数小于最大线程数,直接创建一个非核心的Worker执行当前task
Worker
addWorker
maximumPoolSize
ThreadPoolExecutor
false
true
getTask()
原子操作++count + 1 < capacity
putLock
Runnable
runWorker(this)
takeLock
corePoolSize - 核心线程数maximumPoolSize - 最大线程数keepAliveTime - 存活时间TimeUnit - 时间单元workQueue - 队列threadFactory - DefaultThreadFactoryhandler - 拒绝策略
Condition notEmpty.await()
task
2.1、take-task
dequeue
ReentrantLock
原子操作--count > 1
call
corePoolSize
从队列中获取任务
LinkedBlockingQueue.offer()
ExecutorService
enqueue
task.run()
1、小于核心数:addWorker()-Worker(task)、t=getThreadFactory().newThread(this)-t.start()-Worker.run()-runWorker(this)-this.task.run()注:this --- 当前Worker
executesubmit
Node
workers.add(Work)
4、4种拒绝策略默认AbortPolicy
itemnext
workerCountOf < maximumPoolSize
Condition notFull.signal()
count == capacity
count == 0
task = head.next.itemhead = head.nexthead.next.item = null头结点属于哨兵节点,不用于存储数据。取第二个节点数据,将第二个节点作为新哨兵节点,清空其存储的数据,以此类推
compareAndIncrementWorkerCount自旋、核心线程数+1
while 防止虚假唤醒
thread.start()
BlockingQueue
last = last.next = node尾结点的下一个节点是当前节点当前节点设置为新的尾结点
truecore = true
taskthread
唤醒等待线程
Executor
main线程
notEmpty.signal()
RejectedExecutionHandler
Work.run()
AbstractExecutorService
0 条评论
下一页