• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Thread Deadlocking doubt

 
Ranch Hand
Posts: 71
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 451
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic