Java源码解析阻塞队列ArrayBlockingQueue常用方法
本文基于jdk1.8进行分析
ArrayBlockingQueue的功能简介参考https://www.nhooo.com/article/154211.htm。
首先看一下ArrayBlockingQueue的成员变量。如下图。最主要的成员变量是items,它是一个Object类型的数组用于保存阻塞队列中的元素。其次是takeIndex,putIndex,count,分别表示了从队列获取元素的位置,往队列里放元素的位置和队列中元素的个数。然后是lock,notEmpty和notFull三个和锁相关的成员变量。lock是一个可重入锁,而notEmpty和notFull是和lock绑定的2个Condition。对可重入锁不是很了解的同学,可以参考这篇文章https://www.nhooo.com/article/154207.htm。对可重入锁的理解,是理解ArrayBlockingQueue的基础。也可以这么说,理解了可重入锁,那么在理解ArrayBlockingQueue就很顺利了。
/**Thequeueditems**/ finalObject[]items; /**itemsindexfornexttake,poll,peekorremove**/ inttakeIndex; /**itemsindexfornextput,offer,oradd**/ intputIndex; /**Numberofelementsinthequeue**/ intcount; /** *Concurrencycontrolusestheclassictwo-conditionalgorithm *foundinanytextbook. **/ /**Mainlockguardingallaccess**/ finalReentrantLocklock; /**Conditionforwaitingtakes**/ privatefinalConditionnotEmpty; /**Conditionforwaitingputs**/ privatefinalConditionnotFull; /** *Sharedstateforcurrentlyactiveiterators,ornullifthere *areknownnottobeany.Allowsqueueoperationstoupdate *iteratorstate. **/ transientItrsitrs=null;
接下来介绍ArrayBlockingQueue的主要方法。首先是入队方法。ArrayBlockingQueue的入队方法有好几个,功能略有差异,下面我们逐一介绍各个入队方法。首先看一下put方法,如下图。put方法的功能是,往队列尾部插入指定元素,如果队列已满,那么就等待可用空间。方法的实现过程是,首先判断元素是否非空。然后,进行加锁,加锁后判断队列是否已满。如果已满,则等待不满条件。当被唤醒后,进行入队操作。入队方法中,会唤醒在notEmpty条件上等待的线程。
/**
*Insertsthespecifiedelementatthetailofthisqueue,waiting
*forspacetobecomeavailableifthequeueisfull.
*@throwsInterruptedException{@inheritDoc}
*@throwsNullPointerException{@inheritDoc}
**/
publicvoidput(Ee)throwsInterruptedException{
checkNotNull(e);
finalReentrantLocklock=this.lock;
lock.lockInterruptibly();
try{
while(count==items.length)
notFull.await();
enqueue(e);
}finally{
lock.unlock();
}
}
/**
*Insertselementatcurrentputposition,advances,andsignals.
*Callonlywhenholdinglock.
**/
privatevoidenqueue(Ex){
//assertlock.getHoldCount()==1;
//assertitems[putIndex]==null;
finalObject[]items=this.items;
items[putIndex]=x;
if(++putIndex==items.length)
putIndex=0;
count++;
notEmpty.signal();
}
另一个入队方法是offer,代码如下。这个方法与add方法的区别是,offer方法是立刻返回的,它并不像add方法那样,当队列满时会一直等待。
/**
*Insertsthespecifiedelementatthetailofthisqueueifitis
*possibletodosoimmediatelywithoutexceedingthequeue'scapacity,
*returning{@codetrue}uponsuccessand{@codefalse}ifthisqueue
*isfull.Thismethodisgenerallypreferabletomethod{@link#add},
*whichcanfailtoinsertanelementonlybythrowinganexception.
*@throwsNullPointerExceptionifthespecifiedelementisnull
**/
publicbooleanoffer(Ee){
checkNotNull(e);
finalReentrantLocklock=this.lock;
lock.lock();
try{
if(count==items.length)
returnfalse;
else{
enqueue(e);
returntrue;
}
}finally{
lock.unlock();
}
}
接下来看一下出队方法take,代码如下。首先对可重入锁加锁,然后判断元素个数是否为0.如果为0,则等待不空条件,否则进行出队操作。
publicEtake()throwsInterruptedException{
finalReentrantLocklock=this.lock;
lock.lockInterruptibly();
try{
while(count==0)
notEmpty.await();
returndequeue();
}finally{
lock.unlock();
}
}
ArrayListBlockingqueue中还有其他相关方法,这里就不一一介绍了。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接