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 Thread Deadlocking doubt 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 "Thread Deadlocking doubt" Watch "Thread Deadlocking doubt" New topic
Author

Thread Deadlocking doubt

Jagdev Singh
Ranch Hand

Joined: Jan 12, 2010
Posts: 71

I have this question from OCP practice guide by Bert.


In answer options one correct answer is that there may an program deadlock.

According to me there should not be any deadlock because different objects are used here.

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Did you find out why there is a deathlock if there are two different objects?
I have the same question
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Jagdev Singh wrote:I have this question from OCP practice guide by Bert.


In answer options one correct answer is that there may an program deadlock.

According to me there should not be any deadlock because different objects are used here.


Let's change the adjust method into this equivalent format:

Let's change the view method into this equivalent format:

In this code:


g.adjust (g2) means synchronized (g) on line2 of the modified adjust method
g2.view(g) means synchronized(g2) on line2 of the modified view method
Let's use view as an example, view calls adjust by using z which refers to g.
Let's "translate the code" for view method:

The similar thing for adjust:


When view is called, it locks g2. When adjust is called, it locks g. Then, view wants to lock g, which has been locked by adjust. The adjust wants to lock g2 , which has been locked by view.
Deadlock occurs. For more detail, please refer to KB's book about deadlock.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Deadlocking doubt
 
Similar Threads
Thread
Thread Question
Threads - synchronized methods
Please suggest ways to cause deadlock in this code