1. 程式人生 > >使用Synchronized 模擬死鎖產生的示例

使用Synchronized 模擬死鎖產生的示例

使用synchronized可以為資源加鎖,保證共享資源訪問的同步安全問題。不恰當的使用將會導致死鎖問題。

public class DeadLockTest {
    public static String lock1 = "lock1";
    public static String lock2 = "lock2";
    public static void main(String[] args){
        Thread a = new Thread(new Lock1());
        Thread b = new Thread(new Lock2());
        a.start();
        b.start();
    }
}
class Lock1 implements Runnable{
    @Override
    public void run(){
        try{
            while(true){
            	System.out.println(Thread.currentThread().getName()+" want to get lock1");
                synchronized(DeadLockTest.lock1){
                    System.out.println(Thread.currentThread().getName()+" get lock1");
                    Thread.sleep(100);
                    synchronized(DeadLockTest.lock2){
                        System.out.println(Thread.currentThread().getName()+" get lock2");
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
class Lock2 implements Runnable{
    @Override
    public void run(){
        try{
            while(true){
            	System.out.println(Thread.currentThread().getName()+" want to get lock2");
                synchronized(DeadLockTest.lock2){
                    System.out.println(Thread.currentThread().getName()+" get lock2");
                    Thread.sleep(100);
                    synchronized(DeadLockTest.lock1){
                        System.out.println(Thread.currentThread().getName()+" get lock1");
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

控制檯如下資訊

找到目錄下 cmd下執行 jps


此時程序號為5944,輸入 jstack 5944



發現有死鎖資訊的提示。