Java死锁 代码

/**
 * @auther: Ace Lee
 * @date: 2019/8/28 23:11
 */
public class DeadLock {

    private static final String SOURCE_A = "I am a";
    private static final String SOURCE_B = "I am b";

    public static void deadLock(){

        Thread threada = new Thread(new Runnable() {
            public void run() {
                synchronized (SOURCE_A){
                    System.out.println("get a");
                    try {
                        Thread.sleep(5000);
                        synchronized(SOURCE_B){
                            System.out.println("get b");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });


        Thread threadb = new Thread(new Runnable() {
            public void run() {
                synchronized (SOURCE_B){
                    System.out.println("get b");
                    synchronized(SOURCE_A){
                        System.out.println("get a");
                    }
                }
            }
        });

        threada.start();
        threadb.start();

    }

    public static void main(String[] args) {
        deadLock();
    }
}

 

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