我在讀這個:
https://concurrency.markmail.org/search/?q=ArrayBlockingQueue LinkedBlockingQueue#query:ArrayBlockingQueue LinkedBlockingQueue from:"Doug Lea" page:1 mid:sgx3amdfga7esqul state:results
Doug Lea 在其中說:
通常,當您將某物放入佇列時,您將剛剛分配了該新物。同樣,當你拿出一些東西時,你通常會使用它,然后讓它變成垃圾。在這種情況下,佇列節點的額外分配不會對整體 GC 產生太大影響,因此您不妨選擇 LinkedBlockingQueue 更好的可擴展性。我認為這是最常見的用例。
但是,如果您不分配放入佇列的內容,并且在佇列既不空也不滿時不期望有很多執行緒在爭用,那么 ArrayBlockingQueue 可能會更好地作業。
我想知道是什么Aren't allocating the things put into queues意思?
另外,這句話:don't expect lots of threads to be contending when the queue is neither empty nor full, then ArrayBlockingQueue is likely to work better,有人可以更具體地解釋一下嗎?
uj5u.com熱心網友回復:
對我來說,這似乎意味著以下內容:
-
我想知道是什么
Aren't allocating the things put into queues意思?這是關于他們討論的 GC 開銷。
LinkedBlockingQueue為每個添加的專案創建新的內部Node物件,這意味著它會生成垃圾,并因此導致垃圾回收 (GC)。
Doug 說,通常我們存盤在佇列中的專案是以下物件:- 在它們被添加到佇列之前創建
- 從佇列中洗掉后不久就變得未使用(并且有資格進行 GC)
在這些情況下,專案本身無論如何都會導致 GC,因此
Node物件也需要 GC也不是問題。但是如果你只在一個佇列中存盤長期存在的物件,那么
LinkedBlockingQueue可能會成為導致GC的唯一原因。
那么您可能想ArrayBlockingQueue改用它來避免 GC。
(ArrayBlockingQueue添加/洗掉專案時不創建任何物件) -
另外,這句話:
don't expect lots of threads to be contending when the queue is neither empty nor full, then ArrayBlockingQueue is likely to work better,有人可以更具體地解釋一下嗎?LinkedBlockingQueue使用兩把鎖:一把用于put(),一把用于take()。
ArrayBlockingQueue使用一個鎖兩個put()和take()。
結果當“the queue is neither empty nor full”(即既不需要put()也take()不必等待)并且有兩個執行緒時:一個執行put(),另一個執行take()- 如果佇列是
LinkedBlockingQueue,則執行緒不會相互阻塞 - 如果佇列是
ArrayBlockingQueue,那么一個執行緒將不得不等待另一個執行緒完成
- 如果佇列是
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/335993.html
標籤:爪哇 多线程 并发 java.util.concurrent
上一篇:Python-在Process子類中創建Thread子類的問題-執行緒和多處理
下一篇:如何制作Blazor頁面互斥鎖
