aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question ID :988384705515 JQ+ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question ID :988384705515 JQ+" Watch "Question ID :988384705515 JQ+" New topic
Author

Question ID :988384705515 JQ+

Greg Georges
Ranch Hand

Joined: May 10, 2001
Posts: 116
Consider the following code:
<code>
public class Test extends Thread
{
static Object obj1 = new Object();
static Object obj2 = new Object();
public void m1()
{
synchronized(obj1)
{
System.out.print("1 ");
synchronized(obj2)
{
System.out.println("2");
}
}
}
public void m2()
{
synchronized(obj2)
{
System.out.print("2 ");
synchronized(obj1)
{
System.out.println("1");
}
}
}

public void run()
{
m1();
m2();
}
public static void main(String[] args)
{
new Test().start();
new Test().start();
}
}
</code>
What of the following statements are correct?
a)It may result in deadlock and the program may get stuck
b)There is no potential for deadlock in the code
c)Deadlock may occur but the program will not get stuck as the JVM will resolve the deadlock
d)The program will always print 12,21,12 and 21
e)Nothing can be said for sure
I answered B and D but the answer is A and E. I do not believe that deadlock could occur beacause m1() is called then m2() is called so therefore the lock on obj1 must be released before the lock on obj2 can be acquired. Anyone have any answers?

<b>Greg Georges</b><br /><i>Sun Certified Java Programmer for the Java2 platform (SCJP)</i><br /><i>Sun Certified Java Developer for the Java2 platform (SCJD)</i>
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Greg, there is a potential for deadlock in this code.
Suppose Thread 1 starts and executes m1, grabbing the lock for obj1 and then obj2. Meanwhile Thread 2 starts and sits waiting for the lock on obj1 in order to execute m1.
Thread 1 finishes m1, at which point it has released the locks on both obj1 and obj2, and then proceeds to execute m2, grabbing the lock to obj2. Simultaneously, Thread 2 is now able to grab the lock on obj1 and executes m1.
At this point, Thread 1 has the lock on obj2 and Thread 2 has the lock on obj1. Unfortunately, each Thread now needs the lock of the second object to complete the method they are currently executing -- but the second object is locked by the other Thread. We have deadlock.
The output on your screen in this sequence would be
1 // from Thread 1 in first part of m1
2 // from Thread 1 in second part of m1
2 // from Thread 1 in first part of m2
1 // from Thread 2 in first part of m1
The program would hang at this point.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Greg
Notice the order in m1 the objects are called: obj1 and then obj2 is called while still in the synchronized code of obj1. In m2 they are called obj2 then obj1, again the second object called is called within the synchronized code of the first object called. If the first thread runs all the way through m1 and gets into m2 it will aquire the lock on obj2. If, meanwhile, the second thread gets into m1 it'll get the lock on obj1. Then, because the use of the second object is within the synchronized code of the first, they could both be waiting for each other.
hope this answers your question

Dave


Dave
Greg Georges
Ranch Hand

Joined: May 10, 2001
Posts: 116
Got it, thanks for your help
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
Originally posted by Scott Appleton:
Greg, there is a potential for deadlock in this code.
Suppose Thread 1 starts and executes m1, grabbing the lock for obj1 and then obj2. Meanwhile Thread 2 starts and sits waiting for the lock on obj1 in order to execute m1.
Thread 1 finishes m1, at which point it has released the locks on both obj1 and obj2, and then proceeds to execute m2, grabbing the lock to obj2. Simultaneously, Thread 2 is now able to grab the lock on obj1 and executes m1.
At this point, Thread 1 has the lock on obj2 and Thread 2 has the lock on obj1. Unfortunately, each Thread now needs the lock of the second object to complete the method they are currently executing -- but the second object is locked by the other Thread. We have deadlock.
The output on your screen in this sequence would be
1 // from Thread 1 in first part of m1
2 // from Thread 1 in second part of m1
2 // from Thread 1 in first part of m2
1 // from Thread 2 in first part of m1
The program would hang at this point.

Scott,
I think your explanation is very clear. One more thing I want to mention here is: for this program, we can't be sure whether deadlock will happen or not. It has the potential to turn into deadlock, but also possible to run well. I got the following output when executing it.
Output:
1 2
2 1
1 2
2 1
Amit Madan
Ranch Hand

Joined: Dec 20, 2000
Posts: 32
public class Test11 extends Thread
{
static Object obj1 = new Object();
static Object obj2 = new Object();
public void m1()
{
synchronized(obj1)
{
System.out.print("1 from m1");
synchronized(obj2)
{
System.out.println("\t 2 from m1");
}
}
}
public void m2()
{
synchronized(obj2)
{
System.out.print("2 from m2");
synchronized(obj1)
{
System.out.println("\t 1 from m2");
}
}
}
public void run()
{
m1();
m2();
}
public static void main(String[] args)
{
new Test11().start();
new Test11().start();
}
}
When i run this code each time output is(even if run in different dos windows)
1 from m1 2 from m1
2 from m2 1 from m2
1 from m1 2 from m1
2 from m2 1 from m2
it means d option is correct
please explain
Amit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question ID :988384705515 JQ+
 
Similar Threads
Making sure I have this dead lock concept without statics correct
Object lock Question
Basic thread doubt
Deadlock / Object Lock ???
Threads again...