簡體   English   中英

Java線程死鎖

[英]Deadlock of threads java

我是線程新手,並提出了一個死鎖示例。 我試圖重現死鎖情況,但是代碼運行正常,沒有任何問題。

請指導我哪里錯了。 下面是代碼片段

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");
                }
            }   
        }
    }
}

輸出是這樣的:

第一線程
第一步下一步

第二線程
第二步

這兩個鎖都需要共享才能創建死鎖。 嘗試這個

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);
                }
            }

    }
}

編輯:添加獲取鎖之間的睡眠

正如@Sean Bright所建議的,您在錯誤的地方添加睡眠。

而且,兩個線程中都有兩個anotherLock實例,因為線程第一個和第二個線程都可以獲取自己的anotherLock ,所以它永遠不會死鎖。 因此,您必須讓兩個線程共享相同的anotherLock。

請檢查下面的代碼,希望對您有所幫助。

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");
                }
            }   
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM