最简单的多线程死锁案例代码(Java语言)

package com.thread.test;

public class DeadLock {
    private static Object firstMonitor = new Object();
    private static Object secondMonitor = new Object();
    public static void main(String[] args) {
        new Thread(new Runnable(){
            public void run() {
                while(true)
                {
                    synchronized(firstMonitor)
                    {
                        synchronized(secondMonitor)
                        {
                            System.out.println("Thread1");
                        }
                    }
                }
            }

        }).start();

        new Thread(new Runnable(){
            public void run() {
                while(true)
                {
                    synchronized(secondMonitor)
                    {
                        synchronized(firstMonitor)
                        {
                            System.out.println("Thread2");
                        }
                    }
                }
            }
        }).start();
    }
}

有更简单的案例请在留言中贴上代码,谢谢~

posted on 2015-11-05 18:08  岚之山  阅读(201)  评论(0编辑  收藏  举报

导航