当前位置: 移动技术网 > IT编程>开发语言>c# > 基于C#实现的多生产者多消费者同步问题实例

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了基于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#程序设计的学习有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网