C#多线程交替赋值取值
static AutoResetEvent auto=new AutoResetEvent(false); private static int Count = 100; private static int num = 0; static void Main() { Thread th1 = new Thread(setValue); th1.Name = "th1"; Thread th2 = new Thread(getValue); th2.Name = "th2"; Thread th4 = new Thread(setValue); th4.Name = "th4"; Thread th5 = new Thread(getValue); th5.Name = "th5"; th4.Start(); th5.Start(); Console.ReadLine(); } public static void setValue() { for (int i = 0; i < Count; i++) { num = i; auto.Set(); Console.WriteLine("赋值成功!" + i + "----" + Thread.CurrentThread.Name); Thread.Sleep(1); } } public static void getValue() { while (true) { auto.WaitOne(); Console.WriteLine("取值成功!" + num + "----" + Thread.CurrentThread.Name); } }