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.