CSharp · Console
using System;
using System.Threading.Channels;
using System.Threading.Tasks;
public class Program
{
public static async Task Main()
{
// 容量3、DropOldest モードの Channel
var channel = Channel.CreateBounded<int>(new BoundedChannelOptions(3)
{
FullMode = BoundedChannelFullMode.DropOldest
});
// 5つのデータを書き込む
for (int i = 1; i <= 5; i++)
{
await channel.Writer.WriteAsync(i);
Console.WriteLine($"Written: {i}");
}
channel.Writer.Complete();
// 読み取り
await foreach (var item in channel.Reader.ReadAllAsync())
{
Console.WriteLine($"Read: {item}");
}
}
}