⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 deadlock.java

📁 java网络编程
💻 JAVA
字号:
package multithread;



/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Deadlock extends Thread{

  static Object o1=new Object();
  static Object o2=new Object();

  boolean doO1First;

  public Deadlock(boolean doO1First) {
    this.doO1First=doO1First;
  }

  public static void main(String[] args) {
    Deadlock d1 = new Deadlock(true);
    //d1.start();
    Deadlock d2 = new Deadlock(false);
    d1.start();
    d2.start();
  }

  public void run(){
    for(;;){
      if (doO1First){
        synchronized(o1){
          synchronized(o2){
              System.out.println(Thread.currentThread().getName()+" Got both monitors");
          }
        }
      }else{
        synchronized(o2){
          synchronized(o1){
              System.out.println(Thread.currentThread().getName()+" Got both monitors");
          }
        }

      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -