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

    package html;
    
    import java.util.concurrent.TimeUnit;
    
    /**
     * 模拟死锁
     * 
     * @author jis
     *
     */
    public class KN {
    
        public static void main(String[] args) {
    
            Object o1 = new Object();
            Object o2 = new Object();
    
            new Thread(new Test(o1, o2)).start();
            new Thread(new Test(o2, o1)).start();
        }
    
        static class Test implements Runnable {
            Object o1;
            Object o2;
    
            Test(Object o1, Object o2) {
                this.o1 = o1;
                this.o2 = o2;
            }
    
            @Override
            public void run() {
                synchronized (o1) {
                    System.err.println("enter p1");
                    try {
                        TimeUnit.SECONDS.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (o2) {
                        System.err.println("enter p2");
                    }
                }
                System.err.println("end");
            }
    
        }
    
    }
  • 相关阅读:
    sobel
    构造函数
    #pragma once & ifnde
    #pragma comment
    SET容器
    重载[] int& operator[ ]( )
    仿函数 operator()()
    remove_if erase
    vector
    map
  • 原文地址:https://www.cnblogs.com/jpit/p/7404716.html
Copyright © 2011-2022 走看看