看了ArrayBlockingQueue的源碼public?class?ArrayBlockingQueue<E>?extends?AbstractQueue<E>????????implements?BlockingQueue<E>,?java.io.Serializable?{
?
????final?Object[]?items;??
????int?takeIndex;??
????int?putIndex;???
????int?count;????
????final?ReentrantLock?lock;????/**notEmpty條件對(duì)象,用于通知take方法隊(duì)列已有元素,可執(zhí)行獲取操作?*/
????private?final?Condition?notEmpty;????
????private?final?Condition?notFull;???
???????迭代器
?????*/
????transient?Itrs?itrs?=?null;
????public?void?put(E?e)?throws?InterruptedException?{
?????checkNotNull(e);??????
?????final?ReentrantLock?lock?=?this.lock;??//?????????
??????lock.lockInterruptibly();
??????try?{??????????
??????????while?(count?==?items.length)??????????????//將當(dāng)前調(diào)用線程掛起,添加到notFull條件隊(duì)列中等待喚醒
??????????????notFull.await();
??????????enqueue(e);//如果隊(duì)列沒(méi)有滿直接添加。。
??????}?finally?{
??????????lock.unlock();
??????}
??}
}為甚么類的屬性里沒(méi)有ReentrantLock lock = new ReentrantLock();卻可以在put()中直接指定ReentrantLock lock=this.lock;?
為何ArrayBlockingQueue中的ReentranLock對(duì)象無(wú)需實(shí)例化?
阿Dine
2018-09-08 19:18:43