您的位置:首页 > 其它

多线程---使用ManualResetEvent来控制线程间的同步(实现了消费者和生产者模式)

2010-08-26 22:26 881 查看
http://www.cnblogs.com/heaipin g/archive/2010/08/26/thread_ManualResetEvent.html

Set()-->有信号;

ReSet() -->无信号

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ManualResetEventTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
int result = 0;
Cell cell = new Cell();
ManualResetEvent eventX = new ManualResetEvent(false);
CellProd prod = new CellProd(cell, 20, eventX);
CellCons cons = new CellCons(cell, 20, eventX);
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 ex)
{ Console.WriteLine(ex); result = 1; }
catch (ThreadInterruptedException ex)
{
Console.WriteLine(ex);
result = 1;
}
Environment.ExitCode = result;
}

private void button1_Click(object sender, EventArgs e)
{
int result = 0;
Cell cell = new Cell();
ManualResetEvent eventX = new ManualResetEvent(false);
CellProd prod = new CellProd(cell, 20, eventX);
CellCons cons = new CellCons(cell, 20, eventX);
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 ex)
{ Console.WriteLine(ex); result = 1; }
catch (ThreadInterruptedException ex)
{
Console.WriteLine(ex);
result = 1;
}
Environment.ExitCode = result;
}
}

public class Cell
{
int cellContents;
public int ReadFromCell()//读(消费)
{
lock (this)
{
Console.WriteLine("Consume:{0}", cellContents);
}
return cellContents;
}
public void WriteToCell(int n)//写(生产)
{
lock (this)
{
cellContents = n;
Console.WriteLine("Produce:{0}",
cellContents);
}
}
}
/// <summary>
/// 生产者
/// </summary>
public class CellProd
{
Cell cell;
int quantity = 1;//生产者生产次数
ManualResetEvent eventX;
public CellProd(Cell cell, int request, ManualResetEvent eventX)
{
this.cell = cell;
quantity = request;
this.eventX = eventX;
}
public void ThreadRun()
{
for (int looper = 1; looper <= quantity; looper++)
{
cell.WriteToCell(looper);//生产
eventX.Set();//发信号,告诉消费者我已经生产,你可以消费啦
Thread.Sleep(1000);
}
}
}
/// <summary>
/// 消费者
/// </summary>
public class CellCons
{
Cell cell;
int quantity = 1;
ManualResetEvent eventX;
public CellCons(Cell cell, int request, ManualResetEvent eventX)
{
this.cell = cell;
quantity = request;
this.eventX = eventX;
}
public void ThreadRun()
{
for (int looper = 1; looper <= quantity; looper++)
{
eventX.WaitOne();//一直在苦苦等待生产者生产的产品
cell.ReadFromCell();//等到了,消费
eventX.Reset();//告诉自己,这个东西我已经消费了,        //不能再次消费了,只能WaitOne下一次了
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐