基于C#实现的多生产者多消费者同步问题实例

本文实例讲述了基于C#实现的多生产者多消费者同步问题,分享给大家供大家参考之用。具体代码如下:

// 多个生产者和多个消费者,能生产n个产品的情况

using System;
using System.Threading;

public class HoldIntegerSynchronized{
 private int[] buffer; //缓冲区
 private int occupiedBufferCount = 0;
 private int readPosition = 0 , writePosition = 0;
 //下一个读到的位置和写到的位置
 public HoldIntegerSynchronized(int capacity){
 buffer = new int[capacity];
 }

 public int BufferSize{
 get{
  return buffer.Length;
 }
 }

 public int Buffer{
 get{
  int bufferCopy;
  // 加锁
  lock(this){
  while(occupiedBufferCount == 0){ //多个消费者,所以此处改用while
   Console.WriteLine(Thread.CurrentThread.Name + " tries to read. ");
   DisplayState("Buffer Empty. " + Thread.CurrentThread.Name + " waits.");
   Monitor.Wait(this);
   // 为临界区之外等待的生产者放行,让他来"生产"
   // 一直到生产者生产结束,调用了Monitor.PauseAll()
   // 才能继续执行下去,此时,消费者自动重新获得this的锁
  }
  --occupiedBufferCount;
  bufferCopy = buffer[readPosition];
  readPosition = (readPosition + 1) % buffer.Length;
  DisplayState(Thread.CurrentThread.Name + " reads " + bufferCopy);

  // 通知,让等待的 生产者线程 进入Started状态,如果生产者处于临界区之外,这句话执行完后他仍然在临界区之外
  Monitor.PulseAll(this);

  // 释放锁
  }//lock
  return bufferCopy;
 }

 set{
  // 加锁
  lock(this){
  while(occupiedBufferCount == buffer.Length){
   Console.WriteLine(Thread.CurrentThread.Name + " tries to write. ");
   DisplayState("Buffer Full. " + Thread.CurrentThread.Name + " waits.");
   Monitor.Wait(this);
   // 为临界区之外等待消费者放行,让他来"消费"
   // 一直到消费者调用了Monitor.Pause()
   // 才能继续执行下去,此时,生产者自动重新获得this的锁
  }

  buffer[writePosition] = value;
  ++occupiedBufferCount;
  writePosition = (writePosition + 1) % buffer.Length;
  DisplayState(Thread.CurrentThread.Name + " writes " + value);

  // 通知,让Wait状态的 消费者 进入Started状态,如果消费者处于临界区之外,这句话执行完后他仍然在临界区之外
  Monitor.PulseAll(this);
  // 释放锁
  }
 }
 }

 public void DisplayState(string operation){
 Console.Write("{0,-35}",operation);
 for(int i = 0; i < BufferSize; i++ ){
  int a = readPosition;
  int b = writePosition;
  if( a <= i && i < b) {
  Console.Write("{0,-9}",buffer[i]);
  }else if( b < a && !( b <= i && i < a ) ){
  Console.Write("{0,-9}",buffer[i]);
  }else if( occupiedBufferCount == BufferSize){
  Console.Write("{0,-9}",buffer[i]);
  }else{
  Console.Write("{0,-9}","");
  }
 }
 Console.WriteLine("{0}/r/n",occupiedBufferCount);
 }
}

class Producer{
 private HoldIntegerSynchronized sharedLocation;
 private Random randomSleepTime;

 public Producer(HoldIntegerSynchronized shared,Random random){
 sharedLocation = shared;
 randomSleepTime = random;
 }

 public void Produce(){
 for (int count=0; count<3; count++) {
  Thread.Sleep(randomSleepTime.Next(1,2000));
  sharedLocation.Buffer = randomSleepTime.Next(5,10);
 }
 Console.WriteLine(Thread.CurrentThread.Name + " done producing./r/nTerminating " + Thread.CurrentThread.Name + "./r/n");
 }
}

class Consumer{
 private HoldIntegerSynchronized sharedLocation;
 private Random randomSleepTime;

 public Consumer(HoldIntegerSynchronized shared,Random random){
 sharedLocation = shared;
 randomSleepTime = random;
 }
 public void Consume(){
 int sum = 0;
 for (int count=0; count<4; count++) {
  Thread.Sleep(randomSleepTime.Next(1,2000));
  sum += sharedLocation.Buffer;
 }
 Console.WriteLine(Thread.CurrentThread.Name + " read values totaling:" + sum + "/r/nTerminating " + Thread.CurrentThread.Name + ".");
 }
}

class SharedCell{
 static void Main(string[] args){
 HoldIntegerSynchronized holdInteger = new HoldIntegerSynchronized(5);
 Random random = new Random();
 Thread[] producerThreads = new Thread[4];
 Thread[] consumerThreads = new Thread[3];

 Console.Write("{0,-35}","Operation");
 for(int i = 0;i < holdInteger.BufferSize;i++){
  Console.Write("{0,-9}","Elem " + i);
 }
 Console.WriteLine("Occupied Count/r/n");

 for(int i = 0; i < producerThreads.Length;i++){
  Producer producer = new Producer(holdInteger,random);
  producerThreads[i] = new Thread(new ThreadStart(producer.Produce));
  producerThreads[i].Name = "Producer No." + i;
 }

 for(int i = 0; i < consumerThreads.Length;i++){
  Consumer consumer = new Consumer(holdInteger,random);
  consumerThreads[i] = new Thread(new ThreadStart(consumer.Consume));
  consumerThreads[i].Name = "Consumer No." + i;
 }

 for(int i = 0; i < producerThreads.Length;i++){
  producerThreads[i].Start();
 }

 for(int i = 0; i < consumerThreads.Length;i++){
  consumerThreads[i].Start();
 }
 }
}

希望本文所述对大家C#程序设计的学习有所帮助。

(0)

相关推荐

  • c#(Socket)同步套接字代码示例

    同步客户端套接字示例 下面的示例程序创建一个连接到服务器的客户端.该客户端是用同步套接字生成的,因此挂起客户端应用程序的执行,直到服务器返回响应为止.该应用程序将字符串发送到服务器,然后在控制台显示该服务器返回的字符串. C# using System; using System.Net; using System.Net.Sockets; using System.Text; public class SynchronousSocketClient { public static void S

  • c#实现数据同步的方法(使用文件监控对象filesystemwatcher)

    最近在项目中有这么个需求,就是得去实时获取某个在无规律改变的文本文件中的内容.首先想到的是用程序定期去访问这个文件,因为对实时性要求很高,间隔不能超过1S,而且每次获取到文本内容都要去分发给WEB服务器做别的操作,而那个文本的写入有时候会频繁,1秒可能多次,但是也有可能在相当长一段时间内是没有任何写入的. 这样一来如果每秒都去访问文件的话,一个是IO问题,还有就是每次操作都会引起后端一系列程序的反应,文本在长时间内无写入的话,一秒一次的触发一系列徒劳的事情太不可取了. 最终发现了c#中的File

  • C#同步网络时间的方法实例详解

    本文实例讲述了C#同步网络时间的方法.分享给大家供大家参考.具体分析如下: 客户的机器的系统时间经常出错,导致给他们做的软件无法正常使用,所以后来就加了一个同步网络时间的小功能.实现起来很简单,但是却很使用. 这个小功能就是先获取网络时间,然后将系统的时间修改成从网络获得的时间.下面是具体的实现: 获取网络时间: using System; using System.Collections.Generic; using System.Linq; using System.Text; using

  • C#使用AutoResetEvent实现同步

    前几天碰到一个线程的顺序执行的问题,就是一个异步线程往A接口发送一个数据请求.另外一个异步线程往B接口发送一个数据请求,当A和B都执行成功了,再往C接口发送一个请求.说真的,一直做BS项目,对线程了解,还真不多.就知道AutoResetEvent这个东西和线程有关,用于处理线程切换之类,于是决定用AutoResetEvent来处理上面的问题. 于是网上查找相关资料: 原来,AutoResetEvent在.Net多线程编程中经常用到.当某个线程调用WaitOne方法后,信号处于发送状态,该线程会得

  • c#.net多线程编程教学——线程同步

    随着对多线程学习的深入,你可能觉得需要了解一些有关线程共享资源的问题. .NET framework提供了很多的类和数据类型来控制对共享资源的访问. 考虑一种我们经常遇到的情况:有一些全局变量和共享的类变量,我们需要从不同的线程来更新它们,可以通过使用System.Threading.Interlocked类完成这样的任务,它提供了原子的,非模块化的整数更新操作. 还有你可以使用System.Threading.Monitor类锁定对象的方法的一段代码,使其暂时不能被别的线程访问. System

  • c#线程同步使用详解示例

    在应用程序中使用多个线程的一个好处是每个线程都可以异步执行.对于 Windows 应用程序,耗时的任务可以在后台执行,而使应用程序窗口和控件保持响应.对于服务器应用程序,多线程处理提供了用不同线程处理每个传入请求的能力.否则,在完全满足前一个请求之前,将无法处理每个新请求.然而,线程的异步特性意味着必须协调对资源(如文件句柄.网络连接和内存)的访问.否则,两个或更多的线程可能在同一时间访问相同的资源,而每个线程都不知道其他线程的操作. 线程同步的方式 线程同步有:临界区.互斥区.事件.信号量四种

  • C# 委托的三种调用示例(同步调用 异步调用 异步回调)

    首先,通过代码定义一个委托和下面三个示例将要调用的方法: 复制代码 代码如下: public delegate int AddHandler(int a,int b);    public class 加法类    {        public static int Add(int a, int b)        {            Console.WriteLine("开始计算:" + a + "+" + b);            Thread.Sl

  • C#同步、异步远程下载文件实例

    1.使用HttpWebRequest/HttpWebResonse和WebClient 复制代码 代码如下: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); if (!response.ContentType.ToLower().St

  • 解析C#中委托的同步调用与异步调用(实例详解)

    委托的Invoke方法用来进行同步调用.同步调用也可以叫阻塞调用,它将阻塞当前线程,然后执行调用,调用完毕后再继续向下进行.同步调用的例子: 复制代码 代码如下: using System;using System.Threading;public delegate int AddHandler(int a, int b);public class Foo { static void Main() {  Console.WriteLine("**********SyncInvokeTest***

  • C#应用BindingSource实现数据同步的方法

    本文以实例形式讲述了C#应用BindingSource实现数据同步的方法,对C#数据库程序开发来说具有一定的参考借鉴价值.具体实现方法如下: 下面的代码示例演示如何使用 BindingSource 组件,将三个控件(两个文本框控件和一个 DataGridView 控件)绑定到 DataSet 中的同一列. 该示例演示如何处理 BindingComplete 事件,并确保当一个文本框的文本值更改时,会用正确的值更新其他文本框和 DataGridView 控件. 具体代码如下: // Declare

随机推荐