Skip to content

Instantly share code, notes, and snippets.

@AntonSmolkov
Created December 14, 2022 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonSmolkov/e2df02aa4e556be2a397788d0115f647 to your computer and use it in GitHub Desktop.
Save AntonSmolkov/e2df02aa4e556be2a397788d0115f647 to your computer and use it in GitHub Desktop.
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading.Channels;
var lockObject = new object();
var blockingCollection = new BlockingCollection<int>(10);
var channel = Channel.CreateBounded<int>(new BoundedChannelOptions(10) {SingleReader = true});
var cts = new CancellationTokenSource();
cts.CancelAfter(30000);
while (!cts.IsCancellationRequested)
{
Task.Run(() =>
{
//blockingCollection.Add(0); //Result - 52 threads
//channel.Writer.WriteAsync(0).AsTask().GetAwaiter().GetResult(); // Result - 147 threads
//lock (lockObject) { channel.Writer.WriteAsync(0).AsTask().GetAwaiter().GetResult(); } //Result - 56 threads
//var j = 1 + 1; // Result - 15 threads
});
Console.WriteLine(Process.GetCurrentProcess().Threads.Count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment