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

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. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorial
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
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
w__w__w__.___j_a___va2_s.___c___om | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.