第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

為何ArrayBlockingQueue中的ReentranLock對象無需實例化?

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條件對象,用于通知take方法隊列已有元素,可執(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)??????????????//將當前調(diào)用線程掛起,添加到notFull條件隊列中等待喚醒??????????????
?????????notFull.await();??????????
?????????enqueue(e);//如果隊列沒有滿直接添加。。??????}?finally?{??????????
???????????lock.unlock();??????
???????????????}??
????????????}
?????????}

為甚么類的屬性里沒有ReentrantLock lock = new ReentrantLock();

卻可以在put()中直接指定ReentrantLock lock=this.lock;?


正在回答

1 回答

/**
?*?Creates?an?{@code?ArrayBlockingQueue}?with?the?given?(fixed)
?*?capacity?and?the?specified?access?policy.
?*
?*?@param?capacity?the?capacity?of?this?queue
?*?@param?fair?if?{@code?true}?then?queue?accesses?for?threads?blocked
?*????????on?insertion?or?removal,?are?processed?in?FIFO?order;
?*????????if?{@code?false}?the?access?order?is?unspecified.
?*?@throws?IllegalArgumentException?if?{@code?capacity?<?1}
?*/
public?ArrayBlockingQueue(int?capacity,?boolean?fair)?{
????if?(capacity?<=?0)
????????throw?new?IllegalArgumentException();
????this.items?=?new?Object[capacity];
????lock?=?new?ReentrantLock(fair);
????notEmpty?=?lock.newCondition();
????notFull?=??lock.newCondition();
}

構(gòu)造方法里面初始化了lock對象


0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

為何ArrayBlockingQueue中的ReentranLock對象無需實例化?

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號