欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于C#實(shí)現(xiàn)的多生產(chǎn)者多消費(fèi)者同步問題實(shí)例

 更新時(shí)間:2014年09月10日 12:00:41   投稿:shichen2014  
這篇文章主要介紹了基于C#實(shí)現(xiàn)的多生產(chǎn)者多消費(fèi)者同步問題,包括了加鎖與釋放鎖,以及對(duì)應(yīng)臨界資源的訪問。是比較實(shí)用的技巧,需要的朋友可以參考下

本文實(shí)例講述了基于C#實(shí)現(xiàn)的多生產(chǎn)者多消費(fèi)者同步問題,分享給大家供大家參考之用。具體代碼如下:

// 多個(gè)生產(chǎn)者和多個(gè)消費(fèi)者,能生產(chǎn)n個(gè)產(chǎn)品的情況

using System;
using System.Threading;

public class HoldIntegerSynchronized{
 private int[] buffer; //緩沖區(qū)
 private int occupiedBufferCount = 0;
 private int readPosition = 0 , writePosition = 0;
 //下一個(gè)讀到的位置和寫到的位置
 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){ //多個(gè)消費(fèi)者,所以此處改用while
   Console.WriteLine(Thread.CurrentThread.Name + " tries to read. ");
   DisplayState("Buffer Empty. " + Thread.CurrentThread.Name + " waits.");
   Monitor.Wait(this); 
   // 為臨界區(qū)之外等待的生產(chǎn)者放行,讓他來"生產(chǎn)"
   // 一直到生產(chǎn)者生產(chǎn)結(jié)束,調(diào)用了Monitor.PauseAll()
   // 才能繼續(xù)執(zhí)行下去,此時(shí),消費(fèi)者自動(dòng)重新獲得this的鎖
  }
  --occupiedBufferCount;
  bufferCopy = buffer[readPosition];
  readPosition = (readPosition + 1) % buffer.Length;  
  DisplayState(Thread.CurrentThread.Name + " reads " + bufferCopy);

  // 通知,讓等待的 生產(chǎn)者線程 進(jìn)入Started狀態(tài),如果生產(chǎn)者處于臨界區(qū)之外,這句話執(zhí)行完后他仍然在臨界區(qū)之外
  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); 
   // 為臨界區(qū)之外等待消費(fèi)者放行,讓他來"消費(fèi)"
   // 一直到消費(fèi)者調(diào)用了Monitor.Pause()
   // 才能繼續(xù)執(zhí)行下去,此時(shí),生產(chǎn)者自動(dòng)重新獲得this的鎖
  }

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

  // 通知,讓W(xué)ait狀態(tài)的 消費(fèi)者 進(jìn)入Started狀態(tài),如果消費(fèi)者處于臨界區(qū)之外,這句話執(zhí)行完后他仍然在臨界區(qū)之外
  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();
 }
 }
}

希望本文所述對(duì)大家C#程序設(shè)計(jì)的學(xué)習(xí)有所幫助。

相關(guān)文章

  • C# XML操作類分享

    C# XML操作類分享

    這篇文章主要分享了C# XML操作類的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#使用正則表達(dá)式實(shí)現(xiàn)漢字轉(zhuǎn)拼音

    C#使用正則表達(dá)式實(shí)現(xiàn)漢字轉(zhuǎn)拼音

    這篇文章主要為大家詳細(xì)介紹了C#如何使用正則表達(dá)式實(shí)現(xiàn)漢字轉(zhuǎn)拼音的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • C#實(shí)現(xiàn)餐飲管理系統(tǒng)完整版

    C#實(shí)現(xiàn)餐飲管理系統(tǒng)完整版

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)餐飲管理系統(tǒng)的完整版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • C#實(shí)現(xiàn)DataTable轉(zhuǎn)TXT、CSV文件

    C#實(shí)現(xiàn)DataTable轉(zhuǎn)TXT、CSV文件

    這篇文章介紹了C#實(shí)現(xiàn)DataTable轉(zhuǎn)TXT、CSV文件的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C#獲取指定PDF文件頁數(shù)的方法

    C#獲取指定PDF文件頁數(shù)的方法

    這篇文章主要介紹了C#獲取指定PDF文件頁數(shù)的方法,涉及C#操作pdf文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • Unity實(shí)現(xiàn)蘋果手機(jī)Taptic震動(dòng)

    Unity實(shí)現(xiàn)蘋果手機(jī)Taptic震動(dòng)

    這篇文章主要介紹了Unity實(shí)現(xiàn)蘋果手機(jī)Taptic震動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • WinForm的延時(shí)加載控件概述

    WinForm的延時(shí)加載控件概述

    這篇文章主要介紹了WinForm的延時(shí)加載控件,很實(shí)用的技巧,在C#程序設(shè)計(jì)中有著比較廣泛的應(yīng)用,需要的朋友可以參考下
    2014-08-08
  • C#下載網(wǎng)頁并在控制臺(tái)輸出的方法

    C#下載網(wǎng)頁并在控制臺(tái)輸出的方法

    這篇文章主要介紹了C#下載網(wǎng)頁并在控制臺(tái)輸出的方法,涉及C#基于http協(xié)議進(jìn)行網(wǎng)頁抓取及控制臺(tái)輸出顯示的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • c#橋接模式(bridge結(jié)構(gòu)模式)用法實(shí)例

    c#橋接模式(bridge結(jié)構(gòu)模式)用法實(shí)例

    這篇文章主要介紹了c#橋接模式(bridge結(jié)構(gòu)模式)用法,較為詳細(xì)的分析了橋接模式的原理與用法實(shí)例,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-12-12
  • C#正則表達(dá)式的6個(gè)簡單例子

    C#正則表達(dá)式的6個(gè)簡單例子

    本文介紹了C#中的正則表達(dá)式的六個(gè)例子,都是經(jīng)常用到的,希望通過本文的介紹,能夠給你帶來收獲。
    2015-10-10

最新評(píng)論