using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace ConsoleApplication1{ class Program { private static AutoResetEvent _SyncSingal1 = new AutoResetEvent(false); private static AutoResetEvent _SyncSingal2 = new AutoResetEvent(false); private static int value; private static object sync_locker = new object(); static void Main(string[] args) { // 接收线程 Task.Factory.StartNew(() => { while (true) { if (_SyncSingal1.WaitOne(20)) { lock (sync_locker) { value ; } _SyncSingal2.Set(); } else { // lock (sync_locker) { value ; } }; if (value > int.MaxValue - 5000) break; } }); // 显示线程 Task.Factory.StartNew(() => { while (true) { Thread.Sleep(100); _SyncSingal1.Set(); if( _SyncSingal2.WaitOne(100)) { lock (sync_locker) { Console.WriteLine(value); value ; } } } }); Console.ReadLine(); } }}
评论