The Artima Developer Community
Sponsored Link

Java Buzz Forum
Use Total Ordering of Objects to Avoid Deadlocks

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jonas Bonér

Posts: 77
Nickname: jonas
Registered: Jul, 2003

Jonas Bonér is a ski and jazz addict who tries to squeeze in some time for open source development.
Use Total Ordering of Objects to Avoid Deadlocks Posted: Jul 27, 2005 2:42 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Jonas Bonér.
Original Post: Use Total Ordering of Objects to Avoid Deadlocks
Feed Title: grubbel
Feed URL: http://docs.codehaus.org/dashboard.action
Feed Description: Weblog of Jonas Bonér [jonas AT codehaus DOT org]
Latest Java Buzz Posts
Latest Java Buzz Posts by Jonas Bonér
Latest Posts From grubbel

Advertisement

Writing multi-threaded applications in Java can as you know be quite tricky, and if not done correctly, one can easily end up with deadlock situations.

For example, take a look at this little code snippet. This piece of Java code implements a Node, which has three methods (that is of our interest):

Object get() , void set(Object o) and void swap(Node n)

Here's the implementation:

public class Node {
    private Object value;
    public synchronized Object get() {
        return value;
    }
    public synchronized void set(Object value) {
        this.value = value;
    }
    public synchronized void swap(Node n) {
        Object tmp = get();
        set(n.get());
        n.set(tmp);
    }
   ... // remaining methods omitted 
}

This class might look okay, but it actually suffers from potential deadlock. For example what happens if, one thread T1 invokes n1.swap(n2) while an other one T2 concurrently invokes n2.swap(n1) ?

Thread T1 acquires the lock for node n1, and thread T2 acquires the lock for node n2. Thread T1 now invokes n2.get() (in the swap(..) method). This invocation now has to wait since node n2 is locked by thread T2, this while the reverse holds for thread T2 (e.g. it needs to wait for node n1 which is locked by thread T1). Which means that we have a deadlock.

This program is however easily fixed by using the technique of totally ordering all objects in the system.

public class Node {
    private Object value;
    public synchronized Object get() {
        return value;
    }
    public synchronized void set(Object value) {
        this.value = value;
    }
    public synchronized void doSwap(Node n) {
        Object tmp = get();
        set(n.get());
        n.set(tmp);
    }
    public void swap(Node n) {
        if (this ==  n) {
            return;
        } else if (System.identityHashCode(this) < System.identityHashCode(n)) {
            doSwap(n);
       } else {
           n.doSwap(this);
       }
    }
   ... // remaining methods omitted 
}

In this program, all locks will be acquired in increasing order, guaranteed to be the same for all threads, and thereby avoiding deadlock situations.

Read: Use Total Ordering of Objects to Avoid Deadlocks

Topic: IRA != Irish. Recent Terrorists != Muslims. Previous Topic   Next Topic Topic: Chip & Charles [Flickr]

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use