Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / C#

System.Collections.Concurrent.ConcurrentDictionary: praktická referencia

System.Collections.Concurrent.ConcurrentDictionary: praktická referencia: ConcurrentDictionary<TKey,TValue> podporuje súbežné čítanie a atomické operácie nad jednotlivými záznamami kľúč-hodnota.

Verzia: .NET API; verified on .NET 10.0.14

Syntax

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

ConcurrentDictionary<TKey,TValue> podporuje súbežné čítanie a atomické operácie nad jednotlivými záznamami kľúč-hodnota.

Použite AddOrUpdate, GetOrAdd a TryUpdate pre zdieľané mapy, ktoré sa musia bezpečne meniť medzi vláknami.

Továrne hodnôt sa môžu mimo interných zámkov spustiť viackrát, preto nesmú mať neopakovateľné vedľajšie účinky.

Jedna atomická metóda neurobí atomickou postupnosť metód a kľúč null nie je povolený.

Úplný program skutočne používa System.Collections.Concurrent.ConcurrentDictionary; presne očakávaný stdout je 1\n2.

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

Výsledok

1
2

Úplný program skutočne používa `System.Collections.Concurrent.ConcurrentDictionary`; presne očakávaný stdout je `1\n2`.

Zdroje