110,538
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Threading;
namespace ThreadTest_3
{
class ResetEvent
{
public AutoResetEvent ModifyEvent { get; set; }
public AutoResetEvent PrintEvent { get; set; }
public ResetEvent()
{
ModifyEvent = new AutoResetEvent(false);
PrintEvent = new AutoResetEvent(false);
}
}
class People
{
public string Name { get; set; }
public string Gender { get; set; }
}
class Producer
{
readonly ResetEvent resetEvent;
internal People people { get; set; }
public Producer(People people, ResetEvent resetEvent)
{
this.people = people;
this.resetEvent = resetEvent;
}
public void Produce()
{
for (int i = 0; i < 2000; i++)
{
resetEvent.ModifyEvent.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);
}
resetEvent.PrintEvent.Set();
}
}
}
class Consumer
{
readonly ResetEvent resetEvent;
internal People people { get; set; }
public Consumer(People people, ResetEvent resetEvent)
{
this.people = people;
this.resetEvent = resetEvent;
}
public void Consume()
{
for (int i = 0; i < 2000; i++)
{
resetEvent.PrintEvent.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);
resetEvent.ModifyEvent.Set();
}
}
}
class Program
{
static void Main()
{
ResetEvent resetEvent = new ResetEvent();
People people = new People();
Producer producer = new Producer(people, resetEvent);
Consumer comsumer = new Consumer(people, resetEvent);
Thread a = new Thread(producer.Produce);
Thread b = new Thread(comsumer.Consume);
a.Start();
b.Start();
resetEvent.ModifyEvent.Set();
a.Join();
b.Join();
Console.ReadKey();
}
}
}
namespace ThreadTest {
class People {
public string Name { get; set; }
public string Gender { get; set; }
}
class Producer {
readonly AutoResetEvent Self;
readonly AutoResetEvent Other;
internal People people { get; set; }
public Producer(People people, AutoResetEvent self ,AutoResetEvent other) {
this.people = people;
this.Self = self;
this.Other = other;
}
public void Produce() {
for (int i = 0; i < 2000; i++) {
Self.WaitOne();
if (i % 2 == 0) {
people.Name = "春哥";
people.Gender = "男";
} else {
people.Name = "凤姐";
people.Gender = "女";
}
Other.Set();
}
}
}
class Comsumer {
readonly AutoResetEvent Self;
readonly AutoResetEvent Other;
internal People people { get; set; }
public Comsumer(People people, AutoResetEvent self ,AutoResetEvent other) {
this.people = people;
this.Self = self;
this.Other = other;
}
public void Comsume() {
for (int i = 0; i < 2000; i++) {
Self.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);
Other.Set();
}
}
}
class Program {
static void Main() {
//producerAuto参数为true,保证producer门处于打开状态,让producer先运行.
AutoResetEvent producerAuto= new AutoResetEvent(true);
AutoResetEvent comsumerAuto = new AutoResetEvent(false);
People people = new People();
Producer producer = new Producer(people, producerAuto,comsumerAuto);
Comsumer comsumer = new Comsumer(people, comsumerAuto, producerAuto);
Thread a = new Thread(producer.Produce);
Thread b = new Thread(comsumer.Comsume);
a.Start();
b.Start();
Console.ReadKey();
}
}
}
using System;
using System.Threading;
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 < 2000; 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 < 2000; 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);
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();
}
}
}