tiechui

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

posted on 2010-12-01 16:54  tiechui  阅读(173)  评论(0编辑  收藏  举报