javasynchronizationdeadlockpetri-net

Deadlock without cycle


if I draw a graph that symbolizes all possible calls to blocking functions (java synchronized methods) and I haven't got any cycle in this graph, can I be sure that deadlocks are imposible. Do petri-nets not work like that?

I am not looking for answers like this: Use some monster framework blahblah.

I want to handle my multithreading with synchronized methodes.

EDIT1: The pointed arrows symbolize if one class calls any synchronized method of another class EDIT2:klick @here the example, showing a cycle


Solution

  • No. Consider:

    private static final Semaphore foo = new Semaphore(1);
    private static final Semaphore bar = new Semaphore(1);
    
    private static void one() throws InterruptedException {
        foo.acquire();
        bar.acquire();
        bar.release();
        foo.release();
    }
    
    private static void two() throws InterruptedException {
        bar.acquire();
        foo.acquire();
        foo.release();
        bar.release();
    }
    

    See http://pastebin.com/QfK5ZByj for a runnable example. It deadlocks pretty quickly for me.