Skip to content

Instantly share code, notes, and snippets.

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 huysentruitw/f6f10cc1e9a10f2ef9bd5ab18f0b4f47 to your computer and use it in GitHub Desktop.
Save huysentruitw/f6f10cc1e9a10f2ef9bd5ab18f0b4f47 to your computer and use it in GitHub Desktop.
class Program
{
static ConcurrentDictionary<string, string> dict = new ConcurrentDictionary<string, string>();
static void Main(string[] args)
{
var t1 = new Thread(() =>
{
dict.GetOrAdd("key1", x =>
{
Console.WriteLine("{1} GetOrAdd {0}", x, DateTime.Now);
Thread.Sleep(TimeSpan.FromSeconds(10));
return x;
});
});
var t2 = new Thread(() =>
{
dict.GetOrAdd("key2", x =>
{
Console.WriteLine("{1} GetOrAdd {0}", x, DateTime.Now);
return x;
});
});
var t3 = new Thread(() =>
{
dict.GetOrAdd("key1", x =>
{
Console.WriteLine("{1} GetOrAdd {0}", x, DateTime.Now);
return x;
});
});
t1.Start();
Thread.Sleep(500);
t2.Start();
t3.Start();
t1.Join();
t2.Join();
t3.Join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment