Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / C#

System.Collections.Concurrent.ConcurrentDictionary: praktická reference

System.Collections.Concurrent.ConcurrentDictionary: praktická reference: ConcurrentDictionary<TKey,TValue> podporuje souběžné čtení a atomické operace nad jednotlivými záznamy klíč-hodnota.

Verze: .NET API; verified on .NET 10.0.14

Syntaxe

System.Collections.Concurrent.ConcurrentDictionary /* select a documented member or constructor */

ConcurrentDictionary<TKey,TValue> podporuje souběžné čtení a atomické operace nad jednotlivými záznamy klíč-hodnota.

Použijte AddOrUpdate, GetOrAdd a TryUpdate pro sdílené mapy, které se musí bezpečně měnit mezi vlákny.

Továrny hodnot se mohou mimo interní zámky spustit vícekrát, proto nesmějí mít neopakovatelné vedlejší účinky.

Jedna atomická metoda neučiní atomickou posloupnost metod a klíč null není povolen.

Úplný program skutečně používá System.Collections.Concurrent.ConcurrentDictionary; přesně očekávaný stdout je 1\n2.

Pří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 map = new ConcurrentDictionary<string, int>();
Console.WriteLine(map.AddOrUpdate("x", 1, (_, old) => old + 1));
Console.WriteLine(map.AddOrUpdate("x", 1, (_, old) => old + 1));

Výsledek

1
2

Úplný program skutečně používá `System.Collections.Concurrent.ConcurrentDictionary`; přesně očekávaný stdout je `1\n2`.

Zdroje