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();
        }
    }

  • 相关阅读:
    遗传算法的几种改进
    python3中让程序暂停运行的语句
    python内存增长问题
    关于编程思路
    关于程序中delay函数带来的繁琐问题
    python一切皆对象的理解
    Inspector did not run successfully.
    ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 401 Unauthor.
    决策树的升入浅出-视频
    -bash: /opt/cslc/jdk1.8.0_144/bin/jps: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录
  • 原文地址:https://www.cnblogs.com/tiechui/p/1893639.html
Copyright © 2011-2022 走看看