Advertisement
Advertisement
//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");
}
}
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
//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
}
}
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
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement