zoukankan      html  css  js  c++  java
  • 模拟死锁

    public class MyClass {
        private static Object resourceA = new Object();
        private static Object resourceB = new Object();
        
        public static void main(String args[]) {
          Thread threadA = new Thread( new Runnable() {
              public void run() {
                  System.out.println(Thread.currentThread() + " getting A");
                  synchronized (resourceA) {
                      System.out.println(Thread.currentThread() + " got A");
                      try {
                          Thread.sleep(1000);
                      } catch(InterruptedException e) {
                          e.printStackTrace();
                      }
                      
                      
                      System.out.println(Thread.currentThread() + " getting B");
                      synchronized (resourceB) {
                          System.out.println(Thread.currentThread() + " got B");
                          try {
                              Thread.sleep(1000);
                          } catch(InterruptedException e) {
                              e.printStackTrace();
                          }
                          
                      }
                      
                  }
                  
                  
              }
          } );
          
          Thread threadB = new Thread( new Runnable() {
              public void run() {
                  
                  System.out.println(Thread.currentThread() + " getting B");
                  synchronized (resourceB) {
                      System.out.println(Thread.currentThread() + " got B");
                      try {
                          Thread.sleep(1000);
                      } catch(InterruptedException e) {
                          e.printStackTrace();
                      }
                      
                      
                      System.out.println(Thread.currentThread() + " getting A");
                      synchronized (resourceA) {
                          System.out.println(Thread.currentThread() + " got A");
                          try {
                              Thread.sleep(1000);
                          } catch(InterruptedException e) {
                              e.printStackTrace();
                          }
                          
                      }
                      
                  }
                  
                  
                  
                  
              }
          } );
          
          
          threadA.start();
          threadB.start();
        }
    }
  • 相关阅读:
    JZOJ 3845. 简单题(simple)
    JZOJ 3844. 统计损失(count)
    JZOJ 3843. 寻找羔羊(agnus)
    JZOJ 3833. 平坦的折线
    JZOJ 1956. 矩形
    JZOJ 3832. 在哪里建酿酒厂
    mysql 语法一 :case when详解
    阿里云推荐码
    redis配置文件详解(转)
    压力测试工具 webbench总结
  • 原文地址:https://www.cnblogs.com/silva/p/15705588.html
Copyright © 2011-2022 走看看