我试图模拟一个死锁情况,我将使用 Java Flight Recorder 记录并使用 Java Mission Control 进行检查。
由于某种原因,阻塞的线程未在线程部分下标记为阻塞,Java 任务控制部分中的自动分析也不会报告死锁情况
同时,收集到的线程转储清楚地表明线程被阻塞并发现了 1 个死锁
public class ExternalDeadLock {
private static Lock lock1 = new ReentrantLock();
private static Lock lock2 = new ReentrantLock();
static Runnable task1 = () -> {
while(true) {
try {
lock1.lock();
sleep();
lock2.lock();
System.out.println("task 1 still running");
} finally {
lock1.unlock();
lock2.unlock();
}
}
};
static Runnable task2 = () -> {
while(true) {
try {
lock2.lock();
sleep();
lock1.lock();
System.out.println("task 2 still running");
} finally {
lock1.unlock();
lock2.unlock();
}
}
};
private static void sleep() {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
System.out.println("Press enter to initiate dead lock");
scanner.nextLine();
ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(task1);
executor.submit(task2);
executor.awaitTermination(10000, TimeUnit.DAYS);
}
}
我正在使用具有所有默认设置的标准“分析”指标预设。
我试图了解我在哪里做错了,这使得死锁情况在 Java Mission Control 中变得如此不明显