namespace ThreadTest_2
{
class People
{
public string Name {get; set;}
public string Gender { get; set; }
}
class Producer
{
readonly AutoResetEvent autoResetEvent;
internal People people { get; set; }
public Producer(People people, AutoResetEvent mre)
{
this.people = people;
this.autoResetEvent = mre;
}
public void Produce()
{
for (int i = 0; i < 20; i++)
{
autoResetEvent.WaitOne();
if (i % 2 == 0)
{
people.Name = "春哥";
Thread.Sleep(1);
people.Gender = "男";
Thread.Sleep(1);
}
else
{
people.Name = "凤姐";
Thread.Sleep(1);
people.Gender = "女";
Thread.Sleep(1);
}
autoResetEvent.Set();
}
}
}
class Comsumer
{
readonly AutoResetEvent autoResetEvent;
internal People people { get; set; }
public Comsumer(People people, AutoResetEvent mre)
{
this.people = people;
this.autoResetEvent = mre;
}
public void Comsume()
{
for (int i = 0; i < 20; i++)
{
autoResetEvent.WaitOne();
if (people.Name == "凤姐" && people.Gender != "女")
{
Console.WriteLine("fail ");
break;
}
if (people.Name == "春哥" && people.Gender != "男")
{
Console.WriteLine(" fail");
break;
}
Console.WriteLine(people.Name + "->" + people.Gender+i);
autoResetEvent.Set();
}
}
}
class Program
{
static void Main()
{
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
People people = new People();
Producer producer = new Producer(people, autoResetEvent);
Comsumer comsumer = new Comsumer(people, autoResetEvent);
Thread a = new Thread(producer.Produce);
Thread b = new Thread(comsumer.Comsume);
a.Start();
b.Start();
autoResetEvent.Set();
a.Join();
b.Join();
Console.ReadKey();
}
}
}
其实单个函数功能我大概都调试懂了,但是不要怪我蠢,我就是如何保证第一个是先运行produce,然后,交替运行的看不懂,一开始两个线程开始了,然后进入for语句,等待着宠幸,然后主线程set()函数起作用,起作用不是应该produc和comsume同时开始进行吗,这个里面是怎么确定一定是交替出现的啊,莫非是两个先后顺序的join()????我试了一下,删除join也没有一点点影响,或者有影响只不过是观察不到的罢了,我就是不清楚这个为何能保证交替出现。
{
class People
{
public string Name {get; set;}
public string Gender { get; set; }
}
class Producer
{
readonly AutoResetEvent autoResetEvent;
internal People people { get; set; }
public Producer(People people, AutoResetEvent mre)
{
this.people = people;
this.autoResetEvent = mre;
}
public void Produce()
{
for (int i = 0; i < 20; i++)
{
autoResetEvent.WaitOne();
if (i % 2 == 0)
{
people.Name = "春哥";
Thread.Sleep(1);
people.Gender = "男";
Thread.Sleep(1);
}
else
{
people.Name = "凤姐";
Thread.Sleep(1);
people.Gender = "女";
Thread.Sleep(1);
}
autoResetEvent.Set();
}
}
}
class Comsumer
{
readonly AutoResetEvent autoResetEvent;
internal People people { get; set; }
public Comsumer(People people, AutoResetEvent mre)
{
this.people = people;
this.autoResetEvent = mre;
}
public void Comsume()
{
for (int i = 0; i < 20; i++)
{
autoResetEvent.WaitOne();
if (people.Name == "凤姐" && people.Gender != "女")
{
Console.WriteLine("fail ");
break;
}
if (people.Name == "春哥" && people.Gender != "男")
{
Console.WriteLine(" fail");
break;
}
Console.WriteLine(people.Name + "->" + people.Gender+i);
autoResetEvent.Set();
}
}
}
class Program
{
static void Main()
{
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
People people = new People();
Producer producer = new Producer(people, autoResetEvent);
Comsumer comsumer = new Comsumer(people, autoResetEvent);
Thread a = new Thread(producer.Produce);
Thread b = new Thread(comsumer.Comsume);
a.Start();
b.Start();
autoResetEvent.Set();
a.Join();
b.Join();
Console.ReadKey();
}
}
}
其实单个函数功能我大概都调试懂了,但是不要怪我蠢,我就是如何保证第一个是先运行produce,然后,交替运行的看不懂,一开始两个线程开始了,然后进入for语句,等待着宠幸,然后主线程set()函数起作用,起作用不是应该produc和comsume同时开始进行吗,这个里面是怎么确定一定是交替出现的啊,莫非是两个先后顺序的join()????我试了一下,删除join也没有一点点影响,或者有影响只不过是观察不到的罢了,我就是不清楚这个为何能保证交替出现。