Producer and consumer with a Circular Buffer : Producer Consumer « Thread « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Thread » Producer ConsumerScreenshots 
Producer and consumer with a Circular Buffer
Producer and consumer with a Circular Buffer

/*
Code revised from Book published by 
(C) Copyright 1992-2006 by Deitel & Associates, Inc. and
Pearson Education, Inc. All Rights Reserved.  

*/
using System;
using System.Threading;

public class Producer 
{
   private CircularBuffer sharedLocation;
   private Random randomSleepTime;

   public ProducerCircularBuffer shared, Random random )
   {
      sharedLocation = shared;
      randomSleepTime = random;
   }
   public void Produce()
   {
      for int count = 1; count <= 10; count++ 
      {
         Thread.SleeprandomSleepTime.Next13001 ) );
         sharedLocation.Buffer = count; 
      
      Console.WriteLine"{0} done producing.\nTerminating {0}.", Thread.CurrentThread.Name );
   }
}

public class Consumer
{
   private CircularBuffer sharedLocation;
   private Random randomSleepTime;

   public ConsumerCircularBuffer shared, Random random )
   {
      sharedLocation = shared;
      randomSleepTime = random;
   }
   public void Consume()
   {
      int sum = 0;

      for int count = 1; count <= 10; count++ )
      {
         Thread.SleeprandomSleepTime.Next13001 ) );
         sum += sharedLocation.Buffer;
      }

      Console.WriteLine("{0} read values totaling: {1}.\nTerminating {0}.",
         Thread.CurrentThread.Name, sum );
   }
}


public class CircularBuffer
{
   private int[] buffers = -1, -1, -};

   private int occupiedBufferCount = 0;

   private int readLocation = 0
   private int writeLocation = 0;
   
   public int Buffer
   {
      get
      {
         lock this )
         {
            if occupiedBufferCount == )
            {
               Console.Write"\nAll buffers empty. {0} waits.",Thread.CurrentThread.Name );
               Monitor.Waitthis );
            
            int readValue = buffersreadLocation ];

            Console.Write"\n{0} reads {1} ",Thread.CurrentThread.Name, buffersreadLocation ] );

            --occupiedBufferCount;

            readLocation = readLocation + % buffers.Length;
            Console.WriteCreateStateOutput() );
            Monitor.Pulsethis );

            return readValue;
         }
      }
      set
      {
         lock this )
         {
            if occupiedBufferCount == buffers.Length )
            {
               Console.Write"\nAll buffers full. {0} waits.",Thread.CurrentThread.Name );
               Monitor.Waitthis );
            }
            bufferswriteLocation = value;

            Console.Write"\n{0} writes {1} ",Thread.CurrentThread.Name, bufferswriteLocation ] );

            ++occupiedBufferCount;
            writeLocation = writeLocation + % buffers.Length;
            Console.WriteCreateStateOutput() );
            Monitor.Pulsethis );
         }
      }
   }

   public string CreateStateOutput()
   {
      string output = "(buffers occupied: " + occupiedBufferCount + ")\nbuffers: ";

      for int i = 0; i < buffers.Length; i++ )
         output += " " + string.Format"{0,2}", buffers] ) "  ";

      output += "\n";
      output += "         ";

      for int i = 0; i < buffers.Length; i++ )
         output += "---- ";

      output += "\n";

      output += "         ";

      for int i = 0; i < buffers.Length; i++ 
      {
         if i == writeLocation && 
            writeLocation == readLocation 
            output += " WR  ";
         else if i == writeLocation )
            output += " W   ";
         else if  i == readLocation 
            output += "  R  ";
         else
            output += "     ";
      }

      output += "\n";
      return output;
   }

   static void Mainstring[] args )
   {
      CircularBuffer shared = new CircularBuffer();

      Random random = new Random();

      Console.Writeshared.CreateStateOutput() );

      Producer producer = new Producershared, random );
      Consumer consumer = new Consumershared, random );

      Thread producerThread = new Threadnew ThreadStartproducer.Produce ) );
      producerThread.Name = "Producer";

      Thread consumerThread = new Threadnew ThreadStartconsumer.Consume ) );
      consumerThread.Name = "Consumer";

      producerThread.Start();
      consumerThread.Start();
   }    

           
       
Related examples in the same category
1.Producer and comsumer in a synchronized bufferProducer and comsumer in a synchronized buffer
2.Uses Wait and Pulse to enable producer and consumer threads to cooperate in using a buffer
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.