C#生产者与消费者问题(二)
2013-08-27 12:53
281 查看
/* * Created by SharpDevelop. * User: huangyibiao * Date: 2013/8/27 * Time: 11:12 * * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; using System.Threading; namespace ThreadTest { class Program { public static void Main(string[] args) { // create share resource Resource sharedResource = new Resource(); ProduceThread produceThread = new ProduceThread(sharedResource); ConsumeThread consumeThread = new ConsumeThread(sharedResource); // create two thread Thread producer = new Thread(new ThreadStart(produceThread.Run)); Thread consumer = new Thread(new ThreadStart(consumeThread.Run)); try { producer.Start(); consumer.Start(); producer.Join(); consumer.Join(); } catch (ThreadStateException e) { Console.WriteLine(e.ToString()); } Console.ReadKey(); } } public class ProduceThread { /// <summary> /// share resource /// </summary> Resource _resource; public ProduceThread(Resource res) { _resource = res; } // begin to produce public void Run() { for (int i = 0; i < 10; ++i) { _resource.Produce(); } } } public class ConsumeThread { /// <summary> /// share resource /// </summary> Resource _resource; public ConsumeThread(Resource res) { _resource = res; } public void Run() { for (int i = 0; i < 10; ++i) { _resource.Consume(); } } } public class Resource { int _resourceContent = 0; bool _flag = false; /// <summary> /// consume method /// </summary> public void Consume() { // get access power Monitor.Enter(this); // if this object has been locked by other thread, then wait. if (!_flag) { try { Monitor.Wait(this); } catch (SynchronizationLockException e) { Console.WriteLine(e); } catch (ThreadInterruptedException e) { Console.WriteLine(e); } } // show contents after getting the access power Console.WriteLine("Consumer: {0}", _resourceContent); _flag = false; Monitor.Pulse(this);// tell the waiting thread, Consume method has finished Monitor.Exit(this); // exit the synchronization } /// <summary> /// puroduce method /// </summary> public void Produce() { // get access power Monitor.Enter(this); // if this object has been locked by other thread, then wait. if (_flag) { try { Monitor.Wait(this); } catch (SynchronizationLockException e) { Console.WriteLine(e); } catch (ThreadInterruptedException e) { Console.WriteLine(e); } } // show contents after getting the access power Console.WriteLine("Consumer: {0}", ++_resourceContent); _flag = true; Monitor.Pulse(this);// tell the waiting thread, Consume method has finished Monitor.Exit(this); // exit the synchronization } } }
相关文章推荐
- 用C#线程技术模拟“生产者-消费者”经典进程同步问题
- 生产者消费者问题(C#)
- c#中生产者消费者问题
- C# 生产者和消费者问题使用Monitor同步
- C#生产者与消费者问题
- C#编写的多生产者多消费者同步问题
- C#编写的多生产者多消费者同步问题
- C#编写的多生产者多消费者同步问题
- C#实现“生产者消费者”问题
- [原创]C#编写的多生产者多消费者同步问题
- c# 多线程学习笔记(四)生产者消费者问题
- c#中生产者消费者问题
- c#多线程之生产者与消费者问题的解决
- C# 多线程编程 经典模型 生产者和消费者问题
- 线程之生产者消费者问题(1)
- 生产者/消费者问题的多种Java实现方式
- linux网络编程之posix 线程(三):posix 匿名信号量与互斥锁 示例生产者--消费者问题
- 黑马程序员自学笔记————多线程 线程间通信之生产者消费者问题;
- Thinking in Java---线程通信+三种方式实现生产者消费者问题
- 黑马程序员——多线程操作经典实例:生产者消费者问题