using System; using System.Threading; public class Test { static Thread winner = null; private static void MyFunc() { Thread.Sleep((int)(new Random().NextDouble() * 1000)); Interlocked.CompareExchange(ref winner, Thread.CurrentThread, null); } public static void Main() { Thread thread1 = new Thread(() => MyFunc()); Thread thread2 = new Thread(() => MyFunc()); Thread thread3 = new Thread(() => MyFunc()); thread1.Name = "thread1"; thread2.Name = "thread2"; thread3.Name = "thread3"; thread1.Start(); thread2.Start(); thread3.Start(); thread1.Join(); thread2.Join(); thread3.Join(); Console.WriteLine("The winner is {0}", winner.Name); } }