Created
April 25, 2016 13:14
-
-
Save huysentruitw/f6f10cc1e9a10f2ef9bd5ab18f0b4f47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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