zoukankan      html  css  js  c++  java
  • classic problem: deadlock

    注意: 2个锁是调用关系的, 不是并列关系

    public class DeadLock {

        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            final Object resource1 = "res1";
            final Object resource2 = "res2";
           
            Thread t1 = new Thread() {
                public void run() {
                    synchronized(resource1) {
                        System.out.println("Thread-1 :locked resource1");
                   
    //                    try {
    //                        Thread.sleep(50);
    //                    } catch (InterruptedException ie) {
    //                        ie.printStackTrace();
    //                    }
    //                   
                        synchronized(resource2) {
                            System.out.println("Thread-1 :locked resource2");
                        }
                    }
                   
                }
            };
           
            Thread t2 = new Thread() {
                public void run() {
                    synchronized(resource2) {
                        System.out.println("Thread-2 :locked resource2");
                   
    //                    try {
    //                        Thread.sleep(50);
    //                    } catch (InterruptedException ie) {
    //                        ie.printStackTrace();
    //                    }

                        synchronized(resource1) {
                            System.out.println("Thread-2 :locked resource1");
                        }
                    }
                }
            };
           
            t1.start();
            t2.start();
        }
    }

  • 相关阅读:
    git配置config记住密码
    C#调用c++类的导出函数
    经典算法之直接插入排序
    经典算法之冒泡排序
    经典算法之二分查找
    shell实战之Linux主机系统监控
    shell高级特性-4
    shell实战之tomcat看门狗
    shell函数-3
    shell运算符与流程控制-2
  • 原文地址:https://www.cnblogs.com/tiechui/p/1893639.html
Copyright © 2011-2022 走看看