public static void Main() { Exchanger <string> exchanger = new Exchanger <string>(); TestExchange(exchanger); Console.ReadKey(); }
public static void TestExchange2Threads(Exchanger <string> exchanger) { Thread thread1 = new Thread(() => { string yours; exchanger.Exchange("thread1 message", Timeout.Infinite, out yours); Console.WriteLine("Thread1 received: " + yours); }); Thread thread2 = new Thread(() => { string yours; exchanger.Exchange("thread2 message", Timeout.Infinite, out yours); Console.WriteLine("Thread2 received: " + yours); }); thread1.Start(); thread2.Start(); thread1.Join(); thread2.Join(); }
public static void TestExchange(Exchanger <string> exchanger) { int nThreads = 6; Thread[] threads = new Thread[6]; for (int i = 0; i < nThreads; i++) { int threadId = i; threads[i] = new Thread(() => { string yours; exchanger.Exchange(threadId + " message", Timeout.Infinite, out yours); Console.WriteLine(threadId + " received: " + yours); }); threads[i].Start(); } for (int i = 0; i < nThreads; i++) { threads[i].Join(); } }