Java 线程死锁 实现



import java.util.concurrent.TimeUnit;

public class Sisuo implements Runnable {
    private String sisuoA;
    private String sisuoB;

    Sisuo() {
    }

    public Sisuo(String sisuoA, String sisuoB) {
        this.sisuoA = sisuoA;
        this.sisuoB = sisuoB;
    }


    @Override
    public void run() {
        synchronized (sisuoA) {
            System.out.println(Thread.currentThread().getName() + ">" + sisuoA);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (sisuoB) {
                System.out.println(Thread.currentThread().getName() + ">" + sisuoB);
            }
        }

    }

    public static void main(String[] args) {

        new Thread(new Sisuo("sisuoA", "sisuoB")).start();
        new Thread(new Sisuo("sisuoB", "sisuoA")).start();
    }
}

©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页