c#实现生产者消费者模式
2013-11-17 21:51
357 查看
namespace Alex.MultiThread
{
publicclass Cell
{
int cellContents;
bool readerFlag =false;
publicint ReadFromCell()//读(消费)
{
lock (this)
{
if (!readerFlag)
{
try
{
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
Console.WriteLine("Consume:{0}", cellContents);
readerFlag =false;
Monitor.Pulse(this);
}
return cellContents;
}
publicvoid WriteToCell(int n)//写(生产)
{
lock (this)
{
if (readerFlag)
{
try
{
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
cellContents = n;
Console.WriteLine("Produce:{0}", cellContents);
readerFlag =true;
Monitor.Pulse(this);
}
}
}
///<summary>
/// 生产者
///</summary>
publicclass CellProd
{
Cell cell;
int quantity =1;//生产者生产次数
public CellProd(Cell cell, int request)
{
this.cell = cell;
quantity = request;
}
publicvoid ThreadRun()
{
for(int looper=1;looper<=quantity;looper++)
{
cell.WriteToCell(looper);//生产
}
}
}
///<summary>
/// 消费者
///</summary>
publicclass CellCons
{
Cell cell;
int quantity =1;
public CellCons(Cell cell, int request)
{
this.cell = cell;
quantity = request;
}
publicvoid ThreadRun()
{
for(int looper=1;looper<=quantity;looper++)
{
cell.ReadFromCell();//消费
}
}
}
publicclass MonitorSample
{
publicstaticvoid Main()
{
int result =0;//标志位,0表示程序没有出错,1表示出错
Cell cell =new Cell();
CellProd prod =new CellProd(cell, 20);
CellCons cons =new CellCons(cell, 20);
Thread producer =new Thread(new ThreadStart(prod.ThreadRun));
Thread consumer =new Thread(new ThreadStart(cons.ThreadRun));
try
{
producer.Start();
consumer.Start();
producer.Join();
consumer.Join();
Console.ReadLine();
}
catch (ThreadStartException e)
{
Console.WriteLine(e);
result =1;
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
result =1;
}
Environment.ExitCode = result;
}
}
}
{
publicclass Cell
{
int cellContents;
bool readerFlag =false;
publicint ReadFromCell()//读(消费)
{
lock (this)
{
if (!readerFlag)
{
try
{
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
Console.WriteLine("Consume:{0}", cellContents);
readerFlag =false;
Monitor.Pulse(this);
}
return cellContents;
}
publicvoid WriteToCell(int n)//写(生产)
{
lock (this)
{
if (readerFlag)
{
try
{
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
cellContents = n;
Console.WriteLine("Produce:{0}", cellContents);
readerFlag =true;
Monitor.Pulse(this);
}
}
}
///<summary>
/// 生产者
///</summary>
publicclass CellProd
{
Cell cell;
int quantity =1;//生产者生产次数
public CellProd(Cell cell, int request)
{
this.cell = cell;
quantity = request;
}
publicvoid ThreadRun()
{
for(int looper=1;looper<=quantity;looper++)
{
cell.WriteToCell(looper);//生产
}
}
}
///<summary>
/// 消费者
///</summary>
publicclass CellCons
{
Cell cell;
int quantity =1;
public CellCons(Cell cell, int request)
{
this.cell = cell;
quantity = request;
}
publicvoid ThreadRun()
{
for(int looper=1;looper<=quantity;looper++)
{
cell.ReadFromCell();//消费
}
}
}
publicclass MonitorSample
{
publicstaticvoid Main()
{
int result =0;//标志位,0表示程序没有出错,1表示出错
Cell cell =new Cell();
CellProd prod =new CellProd(cell, 20);
CellCons cons =new CellCons(cell, 20);
Thread producer =new Thread(new ThreadStart(prod.ThreadRun));
Thread consumer =new Thread(new ThreadStart(cons.ThreadRun));
try
{
producer.Start();
consumer.Start();
producer.Join();
consumer.Join();
Console.ReadLine();
}
catch (ThreadStartException e)
{
Console.WriteLine(e);
result =1;
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
result =1;
}
Environment.ExitCode = result;
}
}
}
相关文章推荐
- 事件委托在C#的实现
- Single模式的C#实现
- 如何在 Microsoft Visual C# .NET 中实现 Microsoft Excel 自动化
- C# 通过接口IEnumerator让自己编写的对象/类,实现foreach遍历方法
- C# 使用NPOI 实现Excel的简单导入导出
- c#调用c++带有回调函数方法的实现
- c# networkcomms 3.0实现模拟登陆总结
- C#中comboBox实现三级联动
- C#WinForm 国际化的简单实现,多语言实现
- C#实现http协议GET、POST请求
- c#中通过事件实现按下回车跳转控件
- 在C#中使用装饰器模式和扩展方法实现Fluent Interface
- C#实现Thrift连接池[新]
- 数字金额转换成中文金额(C#实现)
- Asp.net(C#)实现验证码
- C# 中实现 FIFO 缓冲区–ArrayBuffer(环形缓冲区)
- C# 实现快速闪电关机、快速重启
- C#+AE 实现点击查询属性功能
- 不用AJAX实现前台JS调用后台C#方法(小技巧)
- 短信猫的实现(C#)类库开源啦