CKTIS / C#
asynchrony and concurrency: odniesienie praktyczne
asynchrony and concurrency: odniesienie praktyczne: Asynchroniczność zwalnia wątek podczas oczekiwania, a współbieżność pozwala wielu operacjom postępować naraz.
Wersja: C# language and .NET 10 reference runtime
Składnia
// async concurrencyAsynchroniczność zwalnia wątek podczas oczekiwania, a współbieżność pozwala wielu operacjom postępować naraz.
Używaj async/await dla I/O i kontrolowanych prymitywów dla wspólnego stanu.
Async nie gwarantuje równoległości; blokujące Result/Wait może zakleszczyć kontekst.
Wyjątki, anulowanie i częściowe ukończenie wielu Task wymagają jawnej polityki.
Kompletny program rzeczywiście używa asynchrony and concurrency; oczekiwany stdout to dokładnie 5.
Przykłady
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 results = await Task.WhenAll(Task.FromResult(2), Task.Run(() => 3)); Console.WriteLine(results.Sum());
Wynik
5
Kompletny program rzeczywiście używa `asynchrony and concurrency`; oczekiwany stdout to dokładnie `5`.