CKTIS / C#
System.Collections.Concurrent.ConcurrentDictionary: praktična referenca
System.Collections.Concurrent.ConcurrentDictionary: praktična referenca: ConcurrentDictionary<TKey,TValue> podržava istodobna čitanja i atomske operacije nad pojedinim zapisima ključ-vrijednost.
Verzija: .NET API; verified on .NET 10.0.14
Sintaksa
System.Collections.Concurrent.ConcurrentDictionary /* select a documented member or constructor */ConcurrentDictionary<TKey,TValue> podržava istodobna čitanja i atomske operacije nad pojedinim zapisima ključ-vrijednost.
Koristite AddOrUpdate, GetOrAdd i TryUpdate za dijeljene mape koje se moraju sigurno mijenjati među dretvama.
Tvornice vrijednosti mogu se izvesti više puta izvan unutarnjih zaključavanja pa ne smiju imati neponovljive nuspojave.
Jedna atomska metoda ne čini slijed metoda atomskim, a null ključ nije dopušten.
Cijeli program doista koristi System.Collections.Concurrent.ConcurrentDictionary; točno očekivani stdout jest 1\n2.
Primjeri
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));
Rezultat
1 2
Cijeli program doista koristi `System.Collections.Concurrent.ConcurrentDictionary`; točno očekivani stdout jest `1\n2`.