Java 如何可靠地创建和检测线程死锁

Java 如何可靠地创建和检测线程死锁,java,junit,cross-platform,deadlock,mxbean,Java,Junit,Cross Platform,Deadlock,Mxbean,我在tools类中找到了一个方法,该方法应能在运行时检测到死锁的存在: /** * Returns a list of thread IDs that are in a deadlock * @return the IDs or <code>null</code> if there is no * deadlock in the system */ public static String[] getDeadlockedThreads() { Thread

我在tools类中找到了一个方法,该方法应能在运行时检测到死锁的存在:

/**
 * Returns a list of thread IDs that are in a deadlock
 * @return the IDs or <code>null</code> if there is no
 * deadlock in the system
 */
public static String[] getDeadlockedThreads() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    long[] vals = threadBean.findDeadlockedThreads();
    if (vals == null){
        return null;
    }
    String[] ret = new String[vals.length];
    for (int i = 0; i < ret.length; i++){
        ret[i] = Long.toString(vals[i]);
    }
    return ret;
}
我创建了一个JUnit测试来测试该功能。它在Windows上运行良好,但在Linux系统上,测试10次中有8次失败。这是我的测试代码:

/**
 * Tests the correct functionality of the get deadlock info functionality
 * 
 * @throws Exception Will be thrown if there was an error
 *             while performing the test
 */
public void testGetDeadlockInformation() throws Exception {
    assertNull("check non-existance of deadlock", ThreadUtils.getDeadlockedThreads());

    final String monitor1 = "Monitor1";
    final String monitor2 = "Monitor2";

    Thread[] retThreads = createDeadlock(monitor1, monitor2, this);

    String[] res = ThreadUtils.getDeadlockedThreads();
    assertNotNull("check existance of returned deadlock info", res);
    assertEquals("check length of deadlock array", 2, res.length);

    retThreads[0].interrupt();
    retThreads[0].interrupt();
    Thread.sleep(100);

    res = ThreadUtils.getDeadlockedThreads();
    assertNotNull("check existance of returned deadlock info", res);
    assertEquals("check length of deadlock array", 2, res.length);
}

/**
 * Creates a deadlock
 * 
 * @param monitor1 monitor 1 that will be used for synchronization
 * @param monitor2 monitor 2 that will be used for synchronization
 * @param waitMonitor The monitor to be used for internal synchronization
 * @return The threads that should be deadlocked
 * @throws InterruptedException Will be thrown if there was an error
 *             while setting up the deadlock
 */
public static Thread[] createDeadlock(final String monitor1, final String monitor2, Object waitMonitor) throws InterruptedException {
    DeadlockThread dt1 = new DeadlockThread(monitor1, monitor2, waitMonitor);
    DeadlockThread dt2 = new DeadlockThread(monitor2, monitor1, waitMonitor);
    DeadlockThread[] retThreads = new DeadlockThread[] {
            dt1,
            dt2,
    };

    synchronized (waitMonitor) {
        dt1.start();
        waitMonitor.wait(1000);
        dt2.start();
        waitMonitor.wait(1000);
    }
    synchronized (monitor1) {
        synchronized (monitor2) {
            monitor1.notifyAll();
            monitor2.notifyAll();
        }
    }
    Thread.sleep(4000);
    return retThreads;
}

private static class DeadlockThread extends Thread {
    private String monitor1;
    private String monitor2;
    private Object waitMonitor;

    public DeadlockThread(String monitor1, String monitor2, Object waitMonitor) {
        this.monitor1 = monitor1;
        this.monitor2 = monitor2;
        this.waitMonitor = waitMonitor;
        setDaemon(true);
        setName("DeadlockThread for monitor " + monitor1 + " and " + monitor2);
    }

    @Override
    public void run() {
        System.out.println(getName() + ": Running");
        synchronized (monitor1) {
            System.out.println(getName() + ": Got lock for monitor '" + monitor1 + "'");
            synchronized (waitMonitor) {
                waitMonitor.notifyAll();
            }
            try {
                System.out.println(getName() + ": Waiting to get lock on '" + monitor2 + "'");
                monitor1.wait(5000);
                System.out.println(getName() + ": Try to get lock on '" + monitor2 + "'");
                synchronized (monitor2) {
                    monitor2.wait(5000);
                }
                System.out.println(getName() + ": Got lock on '" + monitor2 + "', finished");
            } catch (Exception e) {
                // waiting
            }
        }
    }
}
这是运行testcase时的输出:

DeadlockThread for monitor Monitor1 and Monitor2: Running
DeadlockThread for monitor Monitor1 and Monitor2: Got lock for monitor 'Monitor1'
DeadlockThread for monitor Monitor1 and Monitor2: Waiting to get lock on 'Monitor2'
DeadlockThread for monitor Monitor2 and Monitor1: Running
DeadlockThread for monitor Monitor2 and Monitor1: Got lock for monitor 'Monitor2'
DeadlockThread for monitor Monitor2 and Monitor1: Waiting to get lock on 'Monitor1'
DeadlockThread for monitor Monitor1 and Monitor2: Try to get lock on 'Monitor2'
DeadlockThread for monitor Monitor2 and Monitor1: Try to get lock on 'Monitor1'
根据输出,应该有一个死锁,所以无论我尝试检测死锁的方法是错误的,还是我这里缺少的其他东西,都不能像我预期的那样工作。但是,测试应该一直失败,而不仅仅是大部分时间


在Windows上运行测试时,输出是相同的。

只是猜测。您对
Thread.sleep()
的使用似乎非常可疑。尝试使用某种形式的通信来确定两个线程是否准备好处于死锁状态

未经测试:

   private Thread[] creadDeadlock() throws InterruptedException {
      Thread[] deadLocked = new Thread [2];
      CountDownLatch gate = new CountDownLatch( 2 );
      CountDownLatch ready = new CountDownLatch( 2 );
      Object monitor1 = new Object();
      Object monitor2 = new Object();
      Runnable r1 = () -> {
         synchronized( monitor1 ) {
            try {
               gate.countDown();
               gate.await();
               ready.countDown();
               synchronized( monitor2 ) {
                  wait();
               }
            } catch( InterruptedException ex ) {
               // exit
            }
         }
      };
      Runnable r2 = () -> {
         synchronized( monitor2 ) {
            try {
               gate.countDown();
               gate.await();
               ready.countDown();
               synchronized( monitor1 ) {
                  wait();
               }
            } catch( InterruptedException ex ) {
               // exit
            }
         }
      };

      deadLocked[0] = new Thread( r1 );
      deadLocked[1] = new Thread( r2 );
      deadLocked[0].start();
      deadLocked[1].start();
      ready.await();
      return deadLocked;
   }

尝试增加Thread.sleep(4000)并查看是否有帮助。@nyamiouthegaleanstrope 4000是此尝试的结果。它对MacOS有帮助(将其增加到1000),但如果有更好的解决方案,我认为进一步扩展它是“最后的手段”。只是猜测-可能与monitor1和monitor2是字符串有关。@AndrewS这应该不是问题。同步是在传递的引用上进行的,因此不能混合使用内部引用和非内部引用。在这种情况下,我希望输出会有所不同,因为两个线程都会报告它们在第二个监视器上获得了锁,但事实并非如此。谢谢您的回答。最后的
睡眠
发生在死锁发生后,它应该已经就位,以确保
ThreadMXBean
能够意识到死锁。您的建议已经发生在死锁线程的
run
方法中,方法是等待每个监视器并在
createDeadlock
方法中对它们执行
notifyAll
。您知道如果
wait()
您释放了锁,对吗?是的,但这在这里已经介绍过了。从输出来看,死锁应该存在,因为两个线程都没有输出第二个“get lock on[monitorname]”,如果代码的这一部分出现错误,我会这样想。但我认为这不可靠
waitMonitor
真的什么都不做。您希望线程被调度并运行,但您不能确定。因此,您必须使用更明确的方法来确定某个线程(两个线程)已到达代码中的某个点。trheads使用waitMonitor通知调用方法它们已准备好被死锁(
waitMonitor.notifyAll
在锁定第一个监视器后被调用)。