March 2004
Intermediate to advanced
560 pages
26h 47m
English
using System; using System.Threading; using System.Collections; namespace Samples { public class InterlockedSample { private static Hashtable ht = new Hashtable(); public static void Add(object key, object value) { Monitor.Enter(ht); ht.Add(key, value); Thread.Sleep(1000); Monitor.Exit(ht); } public static void Display() { Monitor.Enter(ht); IDictionaryEnumerator ide = ht.GetEnumerator(); while(ide.MoveNext()) Console.WriteLine("Key: {0}, Value: {1}", ide.Key, ide.Value); Monitor.Exit(ht); } public static void StartHere(Object o) { Add(Interlocked.Increment(ref number), o); if(Interlocked.Decrement(ref threads) == 0) threadsDone.Set(); } static int number = 0; static int threads = 5; static AutoResetEvent threadsDone = new AutoResetEvent(false); ...Read now
Unlock full access