• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Deadlocking doubt

 
Jagdev Singh
Ranch Hand
Posts: 71
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you find out why there is a deathlock if there are two different objects?
I have the same question
 
Helen Ma
Ranch Hand
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic