简体   繁体   English

这是线程死锁吗

[英]is this a thread deadlock

I wanted to intentionally do/test java thread deadlock state so I made a following sample code: 我想故意做/测试Java线程的死锁状态,所以我制作了以下示例代码:

public class TestDeadLock extends Thread{
    private Integer a=new Integer(9);
    public void run(){

        if(Thread.currentThread().getName().equals("t1")){
            XXXX();
        }
        else{
            ZZZZ();
        }
    }

    public void XXXX(){
        System.out.println("inside XXXX");
        synchronized(a){
            a++;
            ZZZZ();
        }
        System.out.println("xxxxxxxxxxxxxxxxxxx");
        //ZZZZ();
    }

    public synchronized void ZZZZ(){
        System.out.println("inside ZZZZ");
        synchronized(a){
            a--;
            XXXX();
        }
        System.out.println("zzzzzzzzzzzzzzzzzzzzz");
    }

    public static void main(String[] args) throws InterruptedException {
        TestDeadLock tdl=new TestDeadLock();
        Thread t1=new Thread(tdl);
        Thread t2=new Thread(tdl);
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();

        Thread.sleep(1000);
        System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="+tdl.a);
    }
}

The output came out to be like : 输出结果如下:

inside XXXX 在XXXX内

inside ZZZZ 内部ZZZZ

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=10 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 10

output is NOT exiting. 输出未退出。

I wanted to know, was it due to threads reached Dead Lock state? 我想知道,是否是由于线程达到了Dead Lock状态? Is it a right example to experience Dead Lock . 体验Dead Lock是一个正确的例子。 Suggest or correct me if I am wrong. 建议或纠正我,如果我错了。

No, you are not experiencing a dead lock. 不,您没有遇到死锁。 You are encountering a StackOverflowError because you are running into an infinite loop. 由于遇到无限循环,因此遇到了StackOverflowError

Note that your method 注意你的方法

public synchronized void ZZZZ() {
  System.out.println("inside ZZZZ");

    XXXX(); // run-time exception
}

is equivalent to 相当于

public void ZZZZ() {
  synchronized(this) {
    System.out.println("inside ZZZZ");

      XXXX(); // run-time exception
  }
}

You are not causing a dead lock because you are working on two different instances. 您不会造成死锁,因为您正在两个不同的实例上工作。 Thread 1 locks t1 , thread 2 locks t2 . 线程1锁定t1 ,线程2锁定t2

Your ZZZZ() method contains a call to XXXX() method and vice-versa. 您的ZZZZ()方法包含对XXXX()方法的调用,反之亦然。 Thus, you have created a never-ending chain of calls that goes: ZZZZ() -> XXXX() -> ZZZZ() -> XXXX() -> etc. 因此,您已经创建了一个永无止境的呼叫链:ZZZZ()-> XXXX()-> ZZZZ()-> XXXX()->等。

Eventually, your stack will grow too large from all the nested method calls that get pushed onto the stack. 最终,从所有嵌套到堆栈的方法调用中,堆栈都会变得太大。 Hence, the exceptions that you are getting. 因此,您得到的异常。

Try this example: 试试这个例子:

public class TestThread {
   public static Object Lock1 = new Object();
   public static Object Lock2 = new Object();

   public static void main(String args[]) {

      ThreadDemo1 T1 = new ThreadDemo1();
      ThreadDemo2 T2 = new ThreadDemo2();
      T1.start();
      T2.start();
   }

   private static class ThreadDemo1 extends Thread {
      public void run() {
         synchronized (Lock1) {
            System.out.println("Thread 1: Holding lock 1...");
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 1: Waiting for lock 2...");
            synchronized (Lock2) {
               System.out.println("Thread 1: Holding lock 1 & 2...");
            }
         }
      }
   }
   private static class ThreadDemo2 extends Thread {
      public void run() {
         synchronized (Lock2) {
            System.out.println("Thread 2: Holding lock 2...");
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 2: Waiting for lock 1...");
            synchronized (Lock1) {
               System.out.println("Thread 2: Holding lock 1 & 2...");
            }
         }
      }
   } 
}

This accurately shows threads reaching deadlock. 这可以准确地显示线程达到死锁状态。

Here is the solution: 解决方法如下:

public class TestThread {
   public static Object Lock1 = new Object();
   public static Object Lock2 = new Object();

   public static void main(String args[]) {

      ThreadDemo1 T1 = new ThreadDemo1();
      ThreadDemo2 T2 = new ThreadDemo2();
      T1.start();
      T2.start();
   }

   private static class ThreadDemo1 extends Thread {
      public void run() {
         synchronized (Lock1) {
            System.out.println("Thread 1: Holding lock 1...");
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 1: Waiting for lock 2...");
            synchronized (Lock2) {
               System.out.println("Thread 1: Holding lock 1 & 2...");
            }
         }
      }
   }
   private static class ThreadDemo2 extends Thread {
      public void run() {
         synchronized (Lock1) {
            System.out.println("Thread 2: Holding lock 1...");
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 2: Waiting for lock 2...");
            synchronized (Lock2) {
               System.out.println("Thread 2: Holding lock 1 & 2...");
            }
         }
      }
   } 
}

Source: http://www.tutorialspoint.com/java/java_thread_deadlock.htm 来源: http//www.tutorialspoint.com/java/java_thread_deadlock.htm

Example given by Jase Pellerin is a good example of dead lock but it has one mistake (Sorry Jase Pellerin , i am sure you did it unintetionally) . Jase Pellerin给出的示例是死锁的一个很好的例子,但是它有一个错误(对不起,Jase Pellerin,我敢肯定您是非理性地做到了)。 Here, both methods are trying to get hold of Lock1 first and then Lock2 . 在这里,这两种方法都试图先获取Lock1 ,然后再Lock2 I think it should be other way around. 我认为应该是相反的方式。

Thread1{

synchronized (Lock1) {

synchronized (Lock2) {}

}
}

Thread2{

 synchronized (Lock2) {

 synchronized (Lock1) {}

 }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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