Bechyňák Petr Bechyňák Petr - 4 years ago 158
Java Question

Deadlock on synchronized methods

I am going through a Jakob Jenkov's concurrency tutorial and don't understand how a deadlock can occur in this piece of code:

public class TreeNode {
TreeNode parent = null;
List children = new ArrayList();

public synchronized void addChild(TreeNode child){
if(!this.children.contains(child)) {

public synchronized void addChildOnly(TreeNode child){

public synchronized void setParent(TreeNode parent){
this.parent = parent;

public synchronized void setParentOnly(TreeNode parent){
this.parent = parent;

Author's explanation says:

First thread 1 calls parent.addChild(child). Since addChild() is
synchronized thread 1 effectively locks the parent object for access
from other treads.

Then thread 2 calls child.setParent(parent). Since setParent() is
synchronized thread 2 effectively locks the child object for acces
from other threads.

As all the 4 methods are "synchronized", all of them are being guarded by "this" object, so IMHO the 2nd thread won't be allowed to acquire the lock. Or am I wrong?

Answer Source

The example states that

Here is an example of a TreeNode class that call synchronized methods in different instances:

So both threads entering synchronized methods will sync on different objects, but then cause a deadlock because both have locked their own objects (parent, child) and then attempt to lock the "opposing" object (child, parent).

The example shows how locking things in the same order is important.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download