File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread DeadLock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread DeadLock" Watch "Thread DeadLock" New topic

Thread DeadLock

Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi All,

OCP Java SE 6 Programmer practice exam --> practice exam 4 --> Question No 28

I can't understand the answer F.

suppose if g.adjust(g2) runs first then it acquire the lock on g object , and g2.view(g) acquire the lock on g2 object , so in both method acquire lock on different object, and when g comes prints the values and then call the view method with reference to g2(which is y on it). as view method acquire lock on g2 object , that will run no matter.

OCPJP 6 (91%)
Deepa Sobin

Joined: May 22, 2011
Posts: 9
Hi Ankit,

You are right that when g.adjust(g2) runs then the lock is acquired on the g object. But within the adjust method when y.view(y) is called g2 gets locked.
Similarly, when g2.view(g) is called it tries to acquire the lock on g2 and the view method will try to lock g object.

So, it is possible that g waits to invoke view (as it needs lock on g2 and g2 is already locked by g2.view(g)) and g2 waits for invoking adjust (as it needs lock on g to invoke adjust method).

Hope this clarifies.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 1120
Hi Ankit Buddy,

adjust and view can be written as:

Replace adjust and view methods with the implementation code, g.adjust(g2) will be executed like this:

g2.view(g) will be executed like this:

In KB book, method1 acquires a and method 2 acquires b. Method 1 waits for b and method2 waits for a. If method2 does not release b, method 1 will wait for b forever.If method1 does not release a, method 2 will wait for a forever.

Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Thanks Himai, i cleared the answer.
I agree. Here's the link:
subject: Thread DeadLock
jQuery in Action, 3rd edition