2016-07-26 135 views
0

我是新来的线程,并提出了一个死锁的例子。 我试图重现死锁方案,但代码正常工作没有任何问题。死锁的线程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"); 
       } 
      } 
     } 
    } 
} 

输出是这样的:

第一线
在第一

第二个线程下一步
在二

+2

获取死锁的“窍门”是锁定交错,因此您应该在两个同步块之间睡眠,而不是在两者之间。 –

+0

不是答案,但是,如果您使用两个不同的Runnable实例来实现两个不同的线程行为,而不是一个具有大if语句的run()方法,那么您的代码(和习惯)将更加面向对象决定要执行哪个行为。 –

+0

@jameslarge感谢您提供的行为..我只是检查是否在上述情况下是可能的死锁 –

回答

1

之间睡眠作为@Sean明亮的建议,你错了地方补充睡眠。

更重要的是,在两个线程中都有两个anotherLock实例,因为Thread First和Second Thread都可以获得自己的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"); 
       } 
      } 
     } 
    } 
} 
1

两个锁需要下一步共享以创造僵局。试试这个

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

    } 
} 

编辑:添加获取锁

+0

你好@pablochan,即使这个代码给出了相同的输出。也在我的代码中,我认为两个线程共享锁。请纠正我,如果我错了 –

+0

我跑了代码,并有一个死锁。只需复制并粘贴即可。 – pablochan

+0

@pablochan。你的两个线程都有另一个锁的实例。 anotherLock不在你的代码中共享。 – Gearon