.NET 6 現在有PriorityQueue<TElement,TPriority>這非常有用。該檔案還不是很清楚(在提出問題時授予該檔案仍然適用于 RC1)是否是執行緒安全的。兩件事情:
它駐留
System.Collections.Generic在System.Collections.Concurrent.它確實有名為
TryDequeueand 的方法TryPeek。當然,它們可能只是在佇列為空時不會拋出例外的方法,但它確實給人一種并發集合的印象。
我可以將它用于多執行緒環境而無需包裝/鎖定(例如在 ASP.NET Core 網站中)嗎?我不知道的任何并發等效項(如果可能,我盡量不使用 3rd-party 包)?
uj5u.com熱心網友回復:
隨著看看源代碼的PriorityQueue.Enqueue,例如,它是顯而易見的,代碼不是執行緒安全的:
public void Enqueue(TElement element, TPriority priority)
{
// Virtually add the node at the end of the underlying array.
// Note that the node being enqueued does not need to be physically placed
// there at this point, as such an assignment would be redundant.
int currentSize = _size ; // <-- BOOM
uj5u.com熱心網友回復:
檔案還不是很清楚
它實際上是。.NET 中的任何內容都不是執行緒安全的,除非在檔案中明確提及。時期。
執行緒安全伴隨著(顯著的)性能開銷,特別是在一般情況下(即不假設特定用途)。因此,“以防萬一”讓所有執行緒都安全是非常愚蠢的。因此,.NET 中的一般概念(從 1.0 開始)即沒有任何東西是執行緒安全的,除非在檔案中明確提到它。
正如你所說,檔案沒有提到執行緒安全。因此,非常清楚非執行緒安全。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359321.html
下一篇:Java同步帳戶示例未按預期作業
