死鎖

干饭达人GoodLucy發表於2024-08-15
  1. jdk自帶命令列工具:jsp + jstack -l pid
  2. jdk自帶視覺化工具:jvisualvm
  3. jdk自帶視覺化工具:jconsole

public class T {

    public static void main(String[] args) throws InterruptedException {
        Object lock1 = new Object();
        Object lock2 = new Object();

        new Thread(() -> {
            try {
                synchronized (lock1) {
                    Thread.sleep(200);
                    synchronized (lock2) {

                    }
                }
            } catch (Exception e) {

            }
        }, "執行緒1").start();

        new Thread(() -> {
            try {
                synchronized (lock2) {
                    Thread.sleep(200);
                    synchronized (lock1) {

                    }
                }
            } catch (Exception e) {

            }
        }, "執行緒2").start();
    }
}

相關文章