Thread Synchronized buffer


How to check the thread Synchronized buffer when two threads are running. If your are using more that one thread. You can find the Synchronized buffer of the thread. The follwoing code will help to find the Thread Synchronized buffer

1. Namespace Part


using System;
using System.Threading;


2. Main Program

public class MySynchronizedBuffer
{
private int mybuffer = -1;

private int myoccupiedBufferCount = 0;

public int myBuffer
{
get
{
Monitor.Enter( this );

if ( myoccupiedBufferCount == 0 )
{
Console.WriteLine(Thread.CurrentThread.Name + " tries to read." );
DisplayState( "Buffer empty. " +Thread.CurrentThread.Name + " waits." );
Monitor.Wait( this );
}
--myoccupiedBufferCount;
DisplayState( Thread.CurrentThread.Name + " reads " + buffer );
Monitor.Pulse( this );
int mybufferCopy = mybuffer;
Monitor.Exit( this );
return mybufferCopy;
}
set
{
Monitor.Enter( this );
if ( myoccupiedBufferCount == 1 )
{
Console.WriteLine(Thread.CurrentThread.Name + " tries to write." );
DisplayState( "Buffer full. " + Thread.CurrentThread.Name + " waits." );
Monitor.Wait( this );
}
mybuffer = value;
++myoccupiedBufferCount;
DisplayState( Thread.CurrentThread.Name + " writes " + buffer );
Monitor.Pulse( this );
Monitor.Exit( this );
}
}

public void DisplayState( string operation )
{
Console.WriteLine( "{0,-35}{1,-9}{2}\n",operation, mybuffer, myoccupiedBufferCount );
}

static void Main( string[] args )
{
MySynchronizedBuffer InsMySynchronizedBuffer = new MySynchronizedBuffer();
Random random = new Random();

Console.WriteLine( "{0,-35}{1,-9}{2}\n","Operation", "Buffer", "Occupied Count" );
InsMySynchronizedBuffer.DisplayState( "Initial state" );

Producer myproducer = new Producer( InsMySynchronizedBuffer, random );
Consumer myconsumer = new Consumer( InsMySynchronizedBuffer, random );

Thread producerThread = new Thread( new ThreadStart( myproducer.Produce ) );
producerThread.Name = "Producer";

Thread consumerThread = new Thread( new ThreadStart( myconsumer.Consume ) );
consumerThread.Name = "Consumer";

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


3. Consumer Class


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

public Consumer( MySynchronizedBuffer shared, Random random )
{
sharedLocation = shared;
randomSleepTime = random;
}

public void Consume()
{
int sum = 0;

for ( int count = 1; count <= 10; count++ )
{
Thread.Sleep( randomSleepTime.Next( 1, 1001 ) );
sum += sharedLocation.Buffer;
}

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


4. Producer Class


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

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




By
Nathan


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: