This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Deadlock / Object Lock ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Deadlock / Object Lock ???" Watch "Deadlock / Object Lock ???" New topic
Author

Deadlock / Object Lock ???

Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
I picked this code from an old post and i have modified it a bit to get a better picture of what is happeneing.
---------------------------------------------
public class Thread1 extends Thread
{
static Object obj1 = new Object();
static Object obj2 = new Object();
public void m1()
{
synchronized(obj1)
{
System.out.print(" syn obj1 in m1 "+this);
synchronized(obj2)
{
System.out.println("syn obj2 in m1"+this);
}
}
}
public void m2()
{
synchronized(obj2)
{
System.out.print("syn obj2 in m2 "+this);
synchronized(obj1)
{
System.out.println("syn obj1 in m2"+this);
}
}
}

public void run()
{
m1();
m2();
}
public static void main(String[] args)
{
new Thread1().start();
new Thread1().start();
}
}
-----------------------------------------
O/P i get is
syn obj1 in m1Thread[Thread-0,5,main]syn obj2 in m1Thread[Thread-0,5,main]
syn obj2 in m2Thread[Thread-0,5,main]syn obj1 in m2Thread[Thread-0,5,main]
syn obj1 in m1 Thread[Thread-1,5,main]syn obj2 in m1Thread[Thread-0,5,main]
syn obj2 in m2Thread[Thread-1,5,main]syn obj1 in m2Thread[Thread-1,5,main]
------------------------------------------
So it is clear from the output to analyze what is happening but can someone explain why it is functioning this way??
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The program you posted illustrates well what NOT to do when using object locks. The fact that this code is deadlock-prone does not mean that deadlocks will arise. Thread scheduling depends on the underlying hardware. When multiple threads are competing for resources, you have no way to tell how the thread execution pattern will look like unless you use methods like join(), etc.
A possible execution sequence could be, for instance:
Thread 1 executes m1()
Thread 1 obtains the lock of obj1
Thread 1 obtains the lock of obj2
Thread 1 releases the lock of obj2
Thread 1 releases the lock of obj1
Thread 1 executes m2()
Thread 1 obtains the lock of obj2
The scheduler decides that Thread 2 is next
Thread 2 executes m1()
Thread 2 obtains the lock of obj1
Thread 2 waits for the lock of obj2 (held by Thread 1)
Thread 1 becomes active again but it must wait for the lock of obj1 (held by Thread 2)
DEADLOCK !!!
Each thread holds a lock of a different object...
The lesson: always allocate object locks in the same order. The modified version of the program below shows the concept:


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
Even when i run ur version of the code it runs fine for me. But i understand that both the versions of the program could go to deadlock state or not which would depend on the platform the program is run.
But tell me in the case of my code, when Thread1 starts exec m1 and obtains lock of obj1. Is it possible for Thread2 to start m2 simutaneously obtaining obj2 lock assuming that while Thread2 was doin this Thread1 was still at obj1.
Thanks for ur time and patience
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Even when i run ur version of the code it runs fine for me.
Because of the platform dependent thing!! Maybe on another platform, you might experience the deadlock earlier.
But i understand that both the versions of the program could go to deadlock state or not which would depend on the platform the program is run.
The second version (the one I provided) cannot go into deadlock
But tell me in the case of my code, when Thread1 starts exec m1 and obtains lock of obj1. Is it possible for Thread2 to start m2 simutaneously obtaining obj2 lock assuming that while Thread2 was doin this Thread1 was still at obj1.
both threads have to execute first m1() and then m2() (in that order) as declared in the run() method.
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
So even if m1 is locked it will just have to wait fine.
The lock and deadlock thing is clear now. The possible execution sequence u gave in teh earlier post was very helpful.
Thanks
 
Consider Paul's rocket mass heater.
 
subject: Deadlock / Object Lock ???
 
Similar Threads
Making sure I have this dead lock concept without statics correct
Question ID :988384705515 JQ+
what's wrong with my code?about synchronized,please help me.
Object lock Question
Thread synchronization