• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question ID :988384705515 JQ+

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greg Georges
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, thanks for your help
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic