CKTIS / C#
System.Collections.Concurrent.ConcurrentBag: odniesienie praktyczne
System.Collections.Concurrent.ConcurrentBag: odniesienie praktyczne: ConcurrentBag<T> jest wielowątkowym, nieuporządkowanym zbiorem zoptymalizowanym pod lokalne Add/TryTake.
Wersja: .NET API; verified on .NET 10.0.14
Składnia
System.Collections.Concurrent.ConcurrentBag /* select a documented member or constructor */ConcurrentBag jest wielowątkowym, nieuporządkowanym zbiorem zoptymalizowanym pod lokalne Add/TryTake.
Stosuj w pulach pracy, gdy kolejność nie ma znaczenia.
Nie zakładaj FIFO ani deterministycznego elementu przy TryTake; Count jest chwilowym obrazem.
TryTake pustego worka daje false; enumeracja jest migawką i nie blokuje późniejszych zmian.
Kompletny program rzeczywiście używa System.Collections.Concurrent.ConcurrentBag; oczekiwany stdout to dokładnie 3.
Przykłady
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
var bag = new ConcurrentBag<int>();
bag.Add(3);
Console.WriteLine(bag.TryTake(out var value) ? value : -1);
Wynik
3
Kompletny program rzeczywiście używa `System.Collections.Concurrent.ConcurrentBag`; oczekiwany stdout to dokładnie `3`.