最简单的多线程死锁案例代码(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();
    }
}

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

你可能感兴趣的:(java,多线程)