using System; using System.Threading;namespace ConsoleApplication1 { class Program { static Mutex mutex = new Mutex(); static number num = new number(); class number { public int a = 0; } class Plus { public void plus() { while (true) { mutex.WaitOne(); num.a++; Console.WriteLine("OK"); mutex.ReleaseMutex(); Thread.Sleep(500); } } } class Show { public void show() { while (true) { mutex.WaitOne(); Console.WriteLine(num.a); mutex.ReleaseMutex(); Thread.Sleep(500); } } } static void Main(string[] args) { Plus plus = new Plus(); Show show = new Show(); Thread thread1 = new Thread(new ThreadStart(plus.plus)); Thread thread2 = new Thread(new ThreadStart(show.show)); thread1.Start(); thread2.Start(); new ManualResetEvent(false).WaitOne(); //按Ctrl+C退出 } } }
这样的同步才是正常的using System; using System.Threading;namespace ConsoleApplication1 { class Program { static AutoResetEvent 生产 = new AutoResetEvent(true); static AutoResetEvent 使用 = new AutoResetEvent(false); static number num = new number(); class number { public int a = 0; } class Plus { public void plus() { while (true) { 生产.WaitOne(); num.a++; Console.WriteLine("OK"); 使用.Set(); Thread.Sleep(500); } } } class Show { public void show() { while (true) { 使用.WaitOne(); Console.WriteLine(num.a); 生产.Set(); } } } static void Main(string[] args) { Plus plus = new Plus(); Show show = new Show(); Thread thread1 = new Thread(new ThreadStart(plus.plus)); Thread thread2 = new Thread(new ThreadStart(show.show)); thread1.Start(); thread2.Start(); new ManualResetEvent(false).WaitOne(); //按Ctrl+C退出 } } }
using System.Threading;namespace ConsoleApplication1
{
class Program
{
static Mutex mutex = new Mutex();
static number num = new number();
class number
{
public int a = 0;
}
class Plus
{
public void plus()
{
while (true)
{
mutex.WaitOne();
num.a++;
Console.WriteLine("OK");
mutex.ReleaseMutex();
Thread.Sleep(500);
}
}
}
class Show
{
public void show()
{
while (true)
{
mutex.WaitOne();
Console.WriteLine(num.a);
mutex.ReleaseMutex();
Thread.Sleep(500);
}
}
}
static void Main(string[] args)
{
Plus plus = new Plus();
Show show = new Show();
Thread thread1 = new Thread(new ThreadStart(plus.plus));
Thread thread2 = new Thread(new ThreadStart(show.show));
thread1.Start();
thread2.Start();
new ManualResetEvent(false).WaitOne(); //按Ctrl+C退出
} }
}
using System.Threading;namespace ConsoleApplication1
{
class Program
{
static AutoResetEvent 生产 = new AutoResetEvent(true);
static AutoResetEvent 使用 = new AutoResetEvent(false);
static number num = new number(); class number
{
public int a = 0;
} class Plus
{
public void plus()
{
while (true)
{
生产.WaitOne();
num.a++;
Console.WriteLine("OK");
使用.Set();
Thread.Sleep(500);
}
}
} class Show
{
public void show()
{
while (true)
{
使用.WaitOne();
Console.WriteLine(num.a);
生产.Set();
}
}
}
static void Main(string[] args)
{
Plus plus = new Plus();
Show show = new Show();
Thread thread1 = new Thread(new ThreadStart(plus.plus));
Thread thread2 = new Thread(new ThreadStart(show.show));
thread1.Start();
thread2.Start();
new ManualResetEvent(false).WaitOne(); //按Ctrl+C退出
} }
}