當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby Queue.close用法及代碼示例

本文簡要介紹ruby語言中 Thread::Queue.close 的用法。

用法

close

關閉隊列。已關閉的隊列無法重新打開。

調用 close 完成後,以下情況屬實:

  • closed? 將返回 true

  • close 將被忽略。

  • 調用 enq/push/<< 將引發 ClosedQueueError

  • empty? 為假時,調用 deq/pop/shift 將照常從隊列中返回一個對象。

  • empty? 為真時,deq(false) 不會掛起線程並返回 nil。 deq(true) 將引發 ThreadError

ClosedQueueError 繼承自 StopIteration ,因此您可以中斷循環塊。

例子:

q = Thread::Queue.new
Thread.new{
  while e = q.deq # wait for nil to break loop
    # ...
  end
}
q.close

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Queue.close。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。