• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic