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.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.