日期:2009-01-17 浏览次数:20531 次
public class MonitorSample { public static void Main(String[] args) { int result = 0; file://一个标志位,如果是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) { file://当线程因为所处状态的原因而不能执行被请求的操作 Console.WriteLine(e); result = 1; } catch (ThreadInterruptedException e) { file://当线程在等待状态的时候中止 Console.WriteLine(e); result = 1; } //尽管Main()函数没有返回值,但下面这条语句可以向父进程返回执行结果 Environment.ExitCode = result; } } |
Produce: 1 Consume: 1 Produce: 2 Consume: 2 Produce: 3 Consume: 3 ... ... Produce: 20 Consume: 20 |
using System; using System.Collections; using System.Threading; //这是用来保存信息的数据结构,将作为参数被传递 public class SomeState { public int Cookie; public SomeState(int iCookie) { Cookie = iCookie; } } public class Alpha { public Hashtable HashCount; public ManualResetEvent eventX; public static int iCount = 0; public static int iMaxCount = 0; public Alpha(int MaxCount) { HashCount = new Hashtable(MaxCount); i
免责声明: 本文仅代表作者个人观点,与爱易网无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
相关资料更多> |