Advertisement



< Prev
Next >



C# Deadlock in Threads






Advertisement




Creating a deadlock situation -:


Let us show you an example of a deadlock situation, which arises when two threads are trying to call methods which are already locked by each of them.

//C# Example of Deadlock


using System;
using System.Threading;

class A 
{
//Defining the method M() of class A
public void M(A a, B b)
{	
	//Locking the access to this method of class A
	//by locking the current object i.e. object of A
	//using this keyword
	lock(this)
	{
		Console.Write(Thread.CurrentThread.Name);
		Console.WriteLine(" is in A's M() method, trying to call B's M() method");
		b.M(a,b);	
	}
}
}


class B 
{

//Defining the method M() of class B
public void M(A a, B b)
{
	//Locking the access to this method of class B
	//by locking the current object i.e. object of B
	//using this keyword
	lock(this)
	{
		Console.Write(Thread.CurrentThread.Name);
		Console.WriteLine(" is in B's M() method, trying to call A's M() method");
		a.M(a,b);
	}
}
}


class C
{
//Instance variables
A a = new A(); 
B b = new B(); 
Thread thread;

C(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 of new thread
	thread.Start();	

	//Main thread calling the method M() of A
	a.M(a,b);  		
}

public void Run()
{
	//New Thread calling the method M() of B
	b.M(a,b);	
}


public static void Main(String []ar)
{
	//Getting the reference of the automatically created Main Thread
	Thread thread = Thread.CurrentThread;

	//Setting the name of the Main thread
	thread.Name = "Main Thread";


	//Creating an object of our C class
	//which is used to construct a new thread
	new C("My Thread1");
}
}


Output is - :


Main Thread is in A's M() method, trying to call B's M() method
My Thread1 is in B's M() method, trying to call A's M() method


Program Analysis


We will create a deadlock situation in the upcoming code by having two threads -




Creating a deadlock situation by locking a private object. -:


Let us show you an example of a deadlock situation, similar to the previous example, but this time we will lock methods of class by locking its private object.

//C# Example of Deadlock


using System;
using System.Threading;

class A 
{
//Creating the private object of Object
Object ob = new Object();

//Defining the method M() of class A
public void M(A a, B b)
{	
	//Locking the access to this method of class A
	//by locking its private object of Object type
	lock(ob)
	{
		Console.Write(Thread.CurrentThread.Name);
		Console.WriteLine(" is in A's M() method, trying to call B's M() method");
		b.M(a,b);	//Line1
	}
}
}


class B 
{
//Creating the private object of Object
Object ob = new Object();

//Defining the method M() of class B
public void M(A a, B b)
{
	//Locking the access to this method of class A
	//by locking its private object of Object type
	lock(ob)
	{
		Console.Write(Thread.CurrentThread.Name);
		Console.WriteLine(" is in B's M() method, trying to call A's M() method");
		a.M(a,b);	//Line2
	}
}
}


class C
{
//Instance variables
A a = new A(); //Line3
B b = new B(); //Line4
Thread thread;

C(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);	//line5

	//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 of new thread
	thread.Start();		//Line6

	a.M(a,b);  		//Line7	
}

public void Run()
{
	b.M(a,b);		//Line8
}


public static void Main(String []ar)
{
	//Creating an object of our C class
	//which is used to construct a separate thread
	new C("My Thread1"); //Line9
}

}


Output is - :


Main Thread is in A's M() method, trying to call B's M() method
My Thread1 is in B's M() method, trying to call A's M() method


Program Analysis


We will create a deadlock situation in the upcoming code by having two threads -


Please share this article -




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



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