ThreadPool
2022-06-23 11:55:56 2 举报
java线程池部分
作者其他创作
大纲/内容
HashSet<Worker> workers.add(w)
t.start(); -> workerStarted = true;
runStateOf(c) != rs
! isRunning(recheck) && remove(command)reject(command)
count.getAndIncrement()if (c + 1 < capacity) notFull.signal();
if (!started) { group.threadStartFailed(this);}
compareAndIncrementWorkerCountCAS 更新+1线程数
w.thread.isAlive()
isRunning(c) && workQueue.offer(command)
if (c == 0)signalNotEmpty(); return c >= 0
ThreadPoolExcutor.excute(Runable)
false
CallerRunsPolicy如果线程池没有Shutdown,则直接调用Runnable任务的run()方法。AbortPolicy丢掉这个任务并抛出 RejectedExecutionException异常。DiscardPolicy忽略这个任务,什么也不做。DiscardOldestPolicy这个策略从字面上也很好理解,丢弃最老的。也就是说如果队列满了,会将最早进入队列的任务删掉腾出空间,再尝试加入队列。因为队列是队尾进,队头出,所以队头元素是最老的,因此每次都是移除对头元素后再尝试入队.
workerCountOf >= (core ? corePoolSize : maximumPoolSize)
return false
group.add(this)started = falsetry{start0()started = true}
if (! workerStarted) addWorkerFailed(w);
new Node<E>(command)enqueue(node) 放入队列末尾
workerAdded == true
w=new work() setstate=-1 new thread()ReentrantLock.lock
largestPoolSize = self<workers?workers.sizeworkerAdded = trueunlock
throw IllegalThreadStateException
0 条评论
下一页