public static void Run() { var v = new ValueHolder <Dictionary <string, string> >(); var wrappedDictionaryFactory = new Func <IEnumerable <Tuple <string, string> >, Func <Dictionary <string, string> > >(xs => { return(new Func <Dictionary <string, string> >(() => { //long run operation... Thread.Sleep(1000 * RandomGenerator.GetRandomInt32(2, 5)); var d = new Dictionary <string, string>(); foreach (var x in xs) { d.Add(x.Item1, x.Item2); } return d; })); }); var ys = new List <Tuple <string, string> >(); for (int i = 0; i < 5; ++i) { ys.Add(Tuple.Create("key #" + i, "value #" + i)); } InstrumentedOperation.Test(() => v.GetValue(wrappedDictionaryFactory(ys)), "creating value"); InstrumentedOperation.Test(() => v.GetValue(wrappedDictionaryFactory(ys)), "getting value"); v.Renew(); InstrumentedOperation.Test(() => v.GetValue(wrappedDictionaryFactory(ys)), "creating value after renew"); for (int i = 5; i < 6; ++i) { ys.Add(Tuple.Create("key #" + i, "value #" + i)); } InstrumentedOperation.Test(() => v.GetValue(wrappedDictionaryFactory(ys), renew: true), "creating value after renew"); var dictionaryFactory = wrappedDictionaryFactory(ys); InstrumentedOperation.Test(() => Parallel.Invoke ( () => v.GetValue(dictionaryFactory), () => v.GetValue(dictionaryFactory), () => v.GetValue(dictionaryFactory) ), "getting values"); InstrumentedOperation.Test(() => Parallel.Invoke ( () => v.GetValue(dictionaryFactory), () => v.GetValue(dictionaryFactory, renew: true), () => v.GetValue(dictionaryFactory) ), "getting values after renew"); }
public static void RunSpawningThreads() { ConsoleEx.WriteLnThreaded("spawning threads..."); InstrumentedOperation.Test(() => { var t = new Thread(() => { //... ConsoleEx.WriteLnThreaded("inside thread..."); //ConsoleEx.ReadLn("press ENTER"); }); t.Start(); t.Join(); }, "System.Threading.Thread"); InstrumentedOperation.Test(() => { const int SIZE = 5; CountdownEvent latch = new CountdownEvent(SIZE); for (int i = 0; i < SIZE; ++i) { ThreadPool.QueueUserWorkItem(state => { var j = (int)state; ConsoleEx.WriteLnThreaded("inside thread pool {0}...", j); Thread.Sleep(RandomGenerator.GetRandomInt32(1000, 5000)); latch.Signal(); }, i + 1); } latch.Wait(); }, "System.Threading.ThreadPool"); var a = new Action(() => { ConsoleEx.WriteLnThreaded("inside Parallel.Invoke..."); Thread.Sleep(RandomGenerator.GetRandomInt32(1000, 5000)); }); InstrumentedOperation.Test(() => { Parallel.Invoke ( a, a, a //, ... ); }, "System.Threading.Tasks.Parallel.Invoke"); InstrumentedOperation.Test(() => { const int SIZE = 50; List <string> xs = new List <string>(SIZE); for (int i = 0; i < SIZE; ++i) { xs.Add(String.Format("inside Parallel.ForEach item #{0}...", i + 1)); } Parallel.ForEach ( xs, x => { ConsoleEx.WriteLnThreaded(x); Thread.Sleep(RandomGenerator.GetRandomInt32(1000, 5000)); } ); }, "System.Threading.Tasks.Parallel.ForEach"); InstrumentedOperation.Test(() => { var a2 = new Action(() => { ConsoleEx.WriteLnThreaded("inside Task..."); Thread.Sleep(RandomGenerator.GetRandomInt32(1000, 5000)); }); var t1 = Task.Factory.StartNew(a2); var t2 = Task.Factory.StartNew(a2); var t3 = Task.Factory.StartNew(a2); var t4 = Task.Factory.StartNew(a2); //... Task.WaitAll(t1, t2, t3, t4); }, "System.Threading.Tasks.Task"); }