多线程学习Demo注解(3)——生产者和消费者
2013-09-04 21:42
344 查看
using System; using System.Threading; public class MonitorSample { public static void Main(String[] args) { int result = 0; //一个标志位,如果是0表示程序没有出错,如果是1表明有错误发生 Cell cell = new Cell(); //下面使用cell初始化CellProd和CellCons两个类,生产和消费次数均为20次 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 (ThreadStateException e) { //当线程因为所处状态的原因而不能执行被请求的操作 Console.WriteLine(e); result = 1; } catch (ThreadInterruptedException e) { //当线程在等待状态的时候中止 Console.WriteLine(e); result = 1; } //尽管Main()函数没有返回值,但下面这条语句可以向父进程返回执行结果 Environment.ExitCode = result; } } public class Cell { int cellContents; // Cell对象里边的内容 bool readerFlag = false; // 状态标志,为true时可以读取,为false则正在写入 public int ReadFromCell() { lock (this) // Lock关键字保证了什么,请大家看前面对lock的介绍 { if (!readerFlag)//如果现在不可读取 { try { //等待WriteToCell方法中调用Monitor.Pulse()方法 Monitor.Wait(this); } catch (SynchronizationLockException e) { Console.WriteLine(e); } catch (ThreadInterruptedException e) { Console.WriteLine(e); } } Console.WriteLine("Consume: {0}", cellContents); readerFlag = false; //重置readerFlag标志,表示消费行为已经完成 Monitor.Pulse(this); //通知WriteToCell()方法(该方法在另外一个线程中执行,等待中) } return cellContents; } public void WriteToCell(int n) { lock (this) { if (readerFlag) { try { Monitor.Wait(this); } catch (SynchronizationLockException e) { //当同步方法(指Monitor类除Enter之外的方法)在非同步的代码区被调用 Console.WriteLine(e); } catch (ThreadInterruptedException e) { //当线程在等待状态的时候中止 Console.WriteLine(e); } } cellContents = n; Console.WriteLine("Produce: {0}", cellContents); readerFlag = true; Monitor.Pulse(this); //通知另外一个线程中正在等待的ReadFromCell()方法 } } } public class CellProd { Cell cell; // 被操作的Cell对象 int quantity = 1; // 生产者生产次数,初始化为1 public CellProd(Cell box, int request) { //构造函数 cell = box; quantity = request; } public void ThreadRun() { for (int looper = 1; looper <= quantity; looper++) cell.WriteToCell(looper); //生产者向操作对象写入信息 } } public class CellCons { Cell cell; int quantity = 1; public CellCons(Cell box, int request) { cell = box; quantity = request; } public void ThreadRun() { int valReturned; for (int looper = 1; looper <= quantity; looper++) valReturned = cell.ReadFromCell();//消费者从操作对象中读取信息 } }
相关文章推荐
- 多线程生产者消费者Demo2_ Lock操作
- Java多线程之生产者消费者demo
- Java多线程学习之 BlockingQueue实现生产者和消费者模型
- 马士兵-多线程学习第05课 生产者消费者问题
- 学习日记1105--多线程间的通信(生产者消费者)(1)
- 学习多线程的生产者和消费者系列的相关实践
- C#多线程学习之(三)生产者和消费者用法分析
- Java多线程学习之生产者消费者问题(一)
- JAVA学习第二十七课(多线程(六))- 多生产者多消费者问题(JDK1.5新特性)
- java多线程学习之生产者与消费者(二)
- java 多线程学习之多生产者多消费者产生的线程安全问题分析与解决:Lock和Condition
- C#多线程学习-----生产者和消费者
- 多线程学习Demo注解(1)
- Java 多线程学习之生产者消费者模型:一个较完善的实现
- 多线程学习——消费者生产者(1)
- 多线程学习Demo注解(2)——lock
- JAVA多线程学习--生产者消费者问题
- 多线程学习——消费者生产者(2)
- C++多线程学习:生产者消费者问题
- java多线程学习生产者消费者