Created
January 12, 2011 05:46
-
-
Save SamSaffron/775753 to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Collections.Concurrent; | |
using System.Threading; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static ConcurrentDictionary<int, string> dict = new ConcurrentDictionary<int, string>(); | |
static void Main(string[] args) | |
{ | |
var rand = new Random(); | |
new Thread(new ThreadStart(() => | |
{ | |
while (true) | |
{ | |
dict.AddOrUpdate(rand.Next(10000), "a", (k, current) => "b"); | |
} | |
})).Start(); | |
new Thread(new ThreadStart(() => | |
{ | |
while (true) | |
{ | |
string ignore; | |
dict.TryRemove(rand.Next(1000), out ignore); | |
} | |
})).Start(); | |
new Thread(new ThreadStart(() => | |
{ | |
while (true) | |
{ | |
Console.WriteLine("size is " + dict.Count); | |
int a = 0; | |
int b = 0; | |
foreach (var pair in dict) | |
{ | |
a += pair.Value == "a" ? 1 : 0; | |
b += pair.Value == "b" ? 1 : 0; | |
} | |
Console.WriteLine("a: " + a + " b: " + b); | |
Thread.Sleep(100); | |
} | |
})).Start(); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment