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

  • 相关阅读:
    后缀数组 POJ 3693 Maximum repetition substring
    后缀数组 POJ 2406 Power Strings
    后缀数组 SPOJ 694 Distinct Substrings
    后缀数组 POJ 3261 Milk Patterns
    后缀数组 POJ 1743 Musical Theme
    后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
    后缀数组 POJ 2217 Secretary
    Codeforces Round #349
    后缀数组 POJ 3581 Sequence
    Codeforces Round #348(VK Cup 2016
  • 原文地址:https://www.cnblogs.com/tiechui/p/1893639.html
Copyright © 2011-2022 走看看