CKTIS / C#
asynchrony and concurrency: практическа справка
asynchrony and concurrency: практическа справка: Асинхронността освобождава нишка при чакане, а конкурентността позволява напредък на няколко операции.
Версия: C# language and .NET 10 reference runtime
Синтаксис
// async concurrencyАсинхронността освобождава нишка при чакане, а конкурентността позволява напредък на няколко операции.
Използвайте async и await за I/O и контролирани примитиви за споделено състояние.
Async не гарантира паралелизъм; блокиращи Result или Wait могат да причинят deadlock.
Изключенията, отмяната и частичното завършване на няколко Task изискват явна политика.
Пълната програма действително използва asynchrony and concurrency; точният очакван stdout е 5.
Примери
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());
Резултат
5
Пълната програма действително използва `asynchrony and concurrency`; точният очакван stdout е `5`.