简体   繁体   中英

Deadlock of threads java

I am new to threads and has come up with a deadlock example. I tried to reproduce a deadlock scenario but the code worked fine without any problem.

Please guide as to where i am wrong. Below is the code snippet

package Practice;

public class Deadlock {

    public static void main(String[] args) {

        Deadlock a = new Deadlock();
        Threadslock first = new Threadslock(a);
        Threadslock second = new Threadslock(a);
        first.setName("First");
        second.setName("Second");
        first.start();
        second.start();

    }

}

class Threadslock extends Thread
{
    Deadlock lock ;
    private String anotherLock = "";
    Threadslock(Deadlock lo)
    {
        lock = lo;
    }
    public void run()
    {

        if(getName().equals("First"))
        {
            synchronized(lock)
            {
                synchronized(anotherLock)
                {
                    try
                    {
                    Thread.sleep (2000);
                    }
                    catch(InterruptedException r)
                    {

                    }
                System.out.println("First Thread");
                System.out.println("Next Step in First");
                }
            }
        }
        else
        {
            synchronized(anotherLock)
            {
                synchronized(lock)
                {
                    try{
                        Thread.sleep (2000);    
                    }
                    catch(Exception e)
                    {

                    }

                System.out.println("Second Thread");
                System.out.println("Next Step in Second");
                }
            }   
        }
    }
}

The output is like this :

First Thread
Next Step in First

Second Thread
Next Step in Second

Both locks need to be shared in order to create a deadlock. Try this

package Practice;

public class Deadlock {

    public static void main(String[] args) {

        Deadlock l1 = new Deadlock();
        Deadlock l2 = new Deadlock();
        Threadslock first = new Threadslock("First", l1, l2);
        Threadslock second = new Threadslock("Second", l2, l1);
        first.start();
        second.start();

    }

}

class Threadslock extends Thread
{
    Deadlock first;
    Deadlock second;
    String name;

    Threadslock(String name, Deadlock first, Deadlock second)
    {
        this.name = name;
        this.first = first;
        this.second = second;
    }
    public void run()
    {

        synchronized(first)
            {
                try
                    {
                    Thread.sleep (2000);
                    }
                    catch(InterruptedException r)
                    {

                    }
                synchronized(second)
                {

                System.out.println(name + " Thread");
                System.out.println("Next Step in " + name);
                }
            }

    }
}

EDIT: Added sleep between acquiring locks

As @Sean Bright suggested, you add sleep in wrong place.

What's more, you have two anotherLock instances in both thread, it will never deadlock since both Thread First and Second Thread can get its own anotherLock . So you must let two threads share the same anotherLock.

Please check the code below ,hope it helps.

public class Deadlock {

    public static void main(String[] args) {

        Deadlock a = new Deadlock();
        String anotherLock = "";
        Threadslock first = new Threadslock(a,anotherLock);
        Threadslock second = new Threadslock(a,anotherLock);
        first.setName("First");
        second.setName("Second");
        first.start();
        second.start();

    }

}

class Threadslock extends Thread
{
    Deadlock lock ;
    String anotherLock;
    Threadslock(Deadlock lo, String anotherLock)
    {
        lock = lo;
        this.anotherLock = anotherLock;
    }
    public void run()
    {

        if(getName().equals("First"))
        {
            synchronized(lock)
            {
                System.out.println("First Thread");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                synchronized(anotherLock)
                {
                    try
                    {
                    Thread.sleep (2000);
                    }
                    catch(InterruptedException r)
                    {

                    }
                System.out.println("Next Step in First");
                }
            }
        }
        else
        {
            synchronized(anotherLock)
            {
                System.out.println("Second Thread");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                synchronized(lock)
                {
                    try{
                        Thread.sleep (2000);    
                    }
                    catch(Exception e)
                    {

                    }
                System.out.println("Next Step in Second");
                }
            }   
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM