2011-10-08 12 views

回答

4

沒有額外的延遲。如果元素可用或線程中斷,則方法調用返回。

Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. 

Returns: 
    the head of this queue 
Throws: 
    InterruptedException - if interrupted while waiting 

BlockinQueue自動這樣做(implement執行。的ArrayBlockingQueue)。

// in add etc. 
notEmpty.signal(); 

// in take() 
while(count == 0) 
    notEmpty.await(); 
+0

@DownVoter:請留下評論。 – MasterCassim

+0

我只能假設downvoter(不,不是我:),反對'沒有延遲' - 可能會有一個調度延遲。消費者線程在生產者發佈項目時已準備好,但如果比消費者更高優先級的就緒/運行線程的集合具有與核心數量相同或更多的元素,則不會立即運行。無論哪種方式,中斷或其他這樣的線程擺弄不會改變任何東西。減少延遲的唯一方法是確保消費者具有足夠高的優先級。 –

+0

感謝您的解釋。我編輯了我的答案,因爲我指的是可能加速的額外延遲。 – MasterCassim