TaskQueue
再来看看队列
public class TaskQueue extends LinkedBlockingQueue {
}
Queue
* @since 1.5
* @author Doug Lea
//还继承了Collection体系
public interface Queue extends Collection {
//增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常
boolean add(E e);
// 添加一个元素并返回true 如果队列已满,则返回false
boolean offer(E e);
// 移除并返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
E remove();
// 移除并返问队列头部的元素 如果队列为空,则返回null
E poll();
//返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
E element();
// 返回队列头部的元素 如果队列为空,则返回null
E peek();
}
TaskQueue
offer
看代码基本就是调用了父类的offer方法,其他方法也是类似 所以分析LinkedBlockingQueue这个类就好了
@Override
public boolean offer(Runnable o) {
//we can’t do any checks
if (parent==null) return super.offer(o);
//we are maxed out on threads, simply queue the object
if (parent.getPoolSize() == parent.getMaximumPoolSize()) return super.offer(o);
//we have idle threads, just add it to the queue
if (parent.getSubmittedCount()<=(parent.getPoolSize())) return super.offer(o);
//if we have less threads than maximum force creation of a new thread
if (parent.getPoolSize()
//if we reached here, we need to add it to the queue
return super.offer(o);
}