成员变量
/** The number of CPUs, for spin control */
static final int NCPUS = Runtime.getRuntime().availableProcessors();
/**
* 定时等待中旋转的此时,经验上cpu超过2个性能便不会变化,所以是一个常数
*/
static final int maxTimedSpins = (NCPUS < 2) ? 0 : 32;
/**
* 在未计时等待中阻塞前要旋转的次数。
*这大于定时值,因为untimed waities spin
*更快,因为他们不需要检查每次旋转的时间。
*/
static final int maxUntimedSpins = maxTimedSpins * 16;
/**
* 旋转速度更快的纳秒数
* 而不是使用定时停车。粗略估计就足够了。
*/
static final long spinForTimeoutThreshold = 1000L;
private ReentrantLock qlock;
private WaitQueue waitingProducers;
private WaitQueue waitingConsumers;
内部类
abstract static class Transferer {
/**
* Performs a put or take.
*
* @param e if non-null, the item to be handed to a consumer;
* if null, requests that transfer return an item
* offered by producer.
* @param timed if this operation should timeout
* @param nanos the timeout, in nanoseconds
* @return if non-null, the item provided or received; if null,
* the operation failed due to timeout or interrupt --
* the caller can distinguish which of these occurred
* by checking Thread.interrupted.
*/
abstract Object transfer(Object e, boolean timed, long nanos);
}
构造函数
public SynchronousQueue() {
this(false);
}
// 默认是一个公平队列
public SynchronousQueue(boolean fair) {
transferer = fair ? new TransferQueue() : new TransferStack();
}
以后在更新剩下的