模拟一个死锁

public class DeadLockDemo {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized ("a"){
                    System.out.println(Thread.currentThread().getName()+"get lock a");
                    synchronized ("b"){
                        System.out.println(Thread.currentThread().getName()+"get lock b");
                    }
                    System.out.println(Thread.currentThread().getName()+"free lock b");
                }
                System.out.println(Thread.currentThread().getName()+"free lock a");
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized ("b"){
                    System.out.println(Thread.currentThread().getName()+"get lock b");
                    synchronized ("a"){
                        System.out.println(Thread.currentThread().getName()+"get lock a");
                    }
                    System.out.println(Thread.currentThread().getName()+"free lock a");
                }
                System.out.println(Thread.currentThread().getName()+"free lock b");
            }
        }).start();
    }
}

你可能感兴趣的:(初学者,java)