using System.Collections.Concurrent;
protected static ConcurrentDictionary<int, object> LockerDictionary = new();
public static void Process(int key)
var locker = LockerDictionary.GetOrAdd(key, new object());
private static void OperateWithKey(int key)
Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} is operating on key {key}");
Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} finished operating on key {key}");
public static void Main()
for (int i = 0; i < 5; i++)
new Thread(() => Process(key)).Start();