Advertisement



< Prev
Next >



C# Interthread Communication



In order to have a smooth interthread communication, we can use three static methods of Monitor class, such as Wait(), Pulse() and PulseAll() methods. The Monitor clsss is defined in System.Threading namespace.




Methods for interthread communication


Methods Description
public static bool Wait(Object obj)
This static method releases the lock on an object and blocks the current thread until it reacquires the lock.

In other words, this method makes the thread that has called the Wait() method to wait for the other thread to complete its work on the same object.

public static void Pulse(Object obj)
This static method notifies the first thread in the waiting queue of threads about a change in the locked object's state.

The thread was waiting(after calling Wait() method) on the same object.

public static void PulseAll(Object obj)
This static method notifies all waiting threads of a change in the object's state i.e. about the release of lock over the object.

These threads were waiting(after calling the Wait() method) on the same object.






Note:


Calling of Wait(), Pulse(), PulseAll() method is only possible from within the synchronized context i.e. from within a synchronized block with a lock.





How Wait() and Pulse() methods help the interthread communication?







No interthread communication without the use of Wait() and Pulse() methods



The manually created new thread will print the table of 5 and the Main Thread will print a table of 6. But, without the use of Wait() and Pulse() methods, when two threads start at almost the same time, The Main Thread may print the table of 6 before the manually created thread has printed the table of 5.

using System;
using System.Threading;


class ThreadEx
{
Thread thread;

//Constructor of our class
public ThreadEx(String name)
{
	//Creating a new thread, based on the object of ThreadEx class
	//Which is referred by 'this' reference
	//We are also specifying the entry-point method that will be called to begin the execution of our thread.
	thread = new Thread(this.Run);

	//Setting the name of a thread by using the Name property of Thread
	thread.Name = name;

	//Calling the Start() method, which calls the entry-point method defined with an argument
	thread.Start();
}

//The entry-point method of the thread
public void Run()
{
	//Synchronizing/locking the object of ThreadEx by using this reference
	//Doing so, restricts any other thread to access a block of code using this object at the same time.
	lock(this)  
	{
		Thread th = Thread.CurrentThread;
		Console.WriteLine(th.Name + " running and printing the table of 5 ");
		for(int i=1; i<=10; i++)
		{
			Console.WriteLine("5 x "+ i + " = " + (5*i));	  
		}    
	}		//synchronized block ends
}

	
public static void Main(String[] ar) 
{
	//Creating an object of our ThreadEx class
	//where each object is used to construct a separate thread
	ThreadEx ob1 = new ThreadEx("My Thread1");
 
	//Synchronizing/locking the access to the object of ThreadEx, referenced by ob1
	//Doing so, restricts any other thread to access a block of code using this object at the same time.
	lock(ob1) 
	{	               
		Thread th = Thread.CurrentThread;
		th.Name = "Main Thread";
		Console.WriteLine(th.Name + " running and printing the table of 6");
		for(int i=1; i<=10; i++)
		{
			Console.WriteLine("6 x "+ i + " = " + (6*i));	  
		}	  
	}	//synchronized block ends
    
}
}

Output -
Main Thread running and printing the table of 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
My Thread1 running and printing the table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50


Program Analysis





Advertisement




Displaying the interthread communication using Wait() and Pulse() methods.



The manually created new thread will print the table of 5 and the Main Thread will print a table of 6. We will use Wait() and Pulse() methods for communication between these two threads, in such a way that table of 5 is printed before table 6, to maintain an ascending order.

using System;
using System.Threading;


class ThreadEx
{
Thread thread;

//Constructor of our class
public ThreadEx(String name)
{
	//Creating a new thread, based on the object of ThreadEx class
	//Which is referred by 'this' reference
	//We are also specifying the entry-point method that will be called to begin the execution of our thread.
	thread = new Thread(this.Run);

	//Setting the name of a thread by using the Name property of Thread
	thread.Name = name;

	//Calling the Start() method, which calls the entry-point method defined with an argument
	thread.Start();
}

//The entry-point method of the thread
public void Run()
{
	//Synchronizing/locking the object of ThreadEx by using this reference
	//Doing so, restricts any other thread to access a block of code using this object at the same time.
	lock(this)  
	{
		Thread th = Thread.CurrentThread;
		Console.WriteLine(th.Name + " running and printing the table of 5 ");
		for(int i=1; i<=10; i++)
		{
			Console.WriteLine("5 x "+ i + " = " + (5*i));	  
		}  

		//The manually created thread is calling the Pulse() method
		//To notifying the Main thread that it is releasing the lock over the object of ThreadEx class
		//And Main Thread could lock the object to continue its work     
		Monitor.Pulse(this);	
	}		//synchronized block ends
}

	
public static void Main(String[] ar) 
{
	//Creating an object of our ThreadEx class
	//where each object is used to construct a separate thread
	ThreadEx ob1 = new ThreadEx("My Thread1");
 
	//Synchronizing/locking the access to the object of ThreadEx, referenced by ob1
	//Doing so, restricts any other thread to access a block of code using this object at the same time.
	lock(ob1) 
	{	               
		//Calling the Wait() method in a synchronized context
		//Doing so, makes the Main Thread stops its execution and wait
		//until it is notified by the Pulse() method
		//on the same object of ThreadEx class
		Monitor.Wait(ob1);
	
		Thread th = Thread.CurrentThread;
		th.Name = "Main Thread";
		Console.WriteLine(th.Name + " running and printing the table of 6");
		for(int i=1; i<=10; i++)
		{
			Console.WriteLine("6 x "+ i + " = " + (6*i));	  
		}	  
	}	//synchronized block ends
    
}
}

Output is - :

My Thread1 running and printing the table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Main Thread running and printing the table of 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60


Program Analysis




Note: In the example above, the call to Wait() or Notify() methods is made from within a synchronized context, otherwise such interthread coummunication would not have been possible.



Please share this article -





< Prev
Next >
< C# Thread Synchronization
C# Thread Interrupt() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement