KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / C#

System.Collections.Concurrent.ConcurrentDictionary: praktische referentie

System.Collections.Concurrent.ConcurrentDictionary: praktische referentie: ConcurrentDictionary<TKey,TValue> ondersteunt gelijktijdig lezen en atomaire bewerkingen op afzonderlijke sleutel-waarde-items.

Versie: .NET API; verified on .NET 10.0.14

Syntax

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

ConcurrentDictionary<TKey,TValue> ondersteunt gelijktijdig lezen en atomaire bewerkingen op afzonderlijke sleutel-waarde-items.

Gebruik AddOrUpdate, GetOrAdd en TryUpdate voor gedeelde maps die veilig tussen threads moeten wijzigen.

Value factories kunnen buiten interne locks meermaals draaien en mogen daarom geen niet-herhaalbare neveneffecten hebben.

Eén atomaire methode maakt een reeks methoden niet atomair en een null-sleutel is niet toegestaan.

Het volledige programma gebruikt System.Collections.Concurrent.ConcurrentDictionary werkelijk; de exact verwachte stdout is 1\n2.

Voorbeelden

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));

Resultaat

1
2

Het volledige programma gebruikt `System.Collections.Concurrent.ConcurrentDictionary` werkelijk; de exact verwachte stdout is `1\n2`.

Bronnen