CKTIS / C#
System.Collections.Concurrent.ConcurrentBag: praktická referencia
System.Collections.Concurrent.ConcurrentBag: praktická referencia: ConcurrentBag<T> je vláknovo bezpečná neusporiadaná kolekcia optimalizovaná pre vlákna, ktoré pridávajú a odoberajú vlastné lokálne prvky.
Verzia: .NET API; verified on .NET 10.0.14
Syntax
System.Collections.Concurrent.ConcurrentBag /* select a documented member or constructor */ConcurrentBag je vláknovo bezpečná neusporiadaná kolekcia optimalizovaná pre vlákna, ktoré pridávajú a odoberajú vlastné lokálne prvky.
Použite ju pre fondy práce alebo objektov, kde nezáleží na poradí a producenti často aj spotrebúvajú.
Nepredpokladajte FIFO ani konkrétny prvok z TryTake; Count je pri súbežných zmenách iba okamžité pozorovanie.
TryTake na prázdnom bagu vráti false a enumerácia vidí snímku, ktorá nezahŕňa neskoršie zmeny.
Úplný program skutočne používa System.Collections.Concurrent.ConcurrentBag; presne očakávaný stdout je 3.
Príklady
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);
Výsledok
3
Úplný program skutočne používa `System.Collections.Concurrent.ConcurrentBag`; presne očakávaný stdout je `3`.