Hi There;
I have a Java code.Three threads have deadlocked themselves.I expect the output in following sequence:
Thread1,Thread2,Thread3
but in case of execution, the following sequence is observed:
Thread1,Thread3,Thread2
Why? Thanks in advance.
Code:
package p95uyg_2;
public class TripleDeadLock {
public static void main(String[] args)
{
final Object resource1 = "resource1";
final Object resource2 = "resource2";
final Object resource3 = "resource3";
Thread t1 = new Thread()
{
public void run()
{
synchronized(resource1)
{
System.out.println("Thread1: locked resource1");
try
{
Thread.sleep(50);
}catch(InterruptedException inex)
{}
synchronized(resource2)
{
System.out.println("Thread1: locked resource2");
}
}
}
};
Thread t2 = new Thread()
{
public void run()
{
synchronized(resource2)
{
System.out.println("Thread2: locked resource2");
try
{
Thread.sleep(50);
}catch(InterruptedException inex)
{}
synchronized(resource3)
{
System.out.println("Thread2: locked resource3");
}
}
}
};
Thread t3 = new Thread()
{
public void run()
{
synchronized(resource3)
{
System.out.println("Thread3: locked resource3");
try
{
Thread.sleep(50);
}catch(InterruptedException inex)
{}
synchronized(resource1)
{
System.out.println("Thread1: locked resource3");
}
}
}
};
t1.start();
t2.start();
t3.start();
}
}