Sunday, April 25, 2010

Dangling Locks

Happy about Java not suffering from dangling pointers ? Think again the Java5 concurrency utils while very powerful looks like a language flaw:
The following code is an example where a lock cannot be released anymore the thread which holds it is dead. Moreover there is no indication who is holding this lock ...... jstack won't help you......
Oracle please fix it !!!


public class DanglingLock {
    static ReadWriteLock l = new ReentrantReadWriteLock();
    public static void main(String [] args) throws InterruptedException  {
        Runnable r = new Runnable() {


            public void run() {
                l.writeLock().lock();
                try {
                    Thread.sleep(20000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(DanglingLock.class.getName()).log(Level.SEVERE, null, ex);
                }
            }


        };
        Thread t = new Thread(r);
        t.start();
                            Thread.sleep(5000);
        l.writeLock().lock();
    }


}

No comments: