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();
        }
    }
  • 相关阅读:
    kickstart自动化安装
    linux双网卡绑定
    作死实验,删除libc.so.6
    安装centos6及安装redhat6后的配置
    交换机、linux光衰查询
    ansible的简单使用
    linux安全配置学习
    vm的三种网络模式
    Recylerview的使用系列教程
    Android自定义组合控件
  • 原文地址:https://www.cnblogs.com/silva/p/15705588.html
Copyright © 2011-2022 走看看