如何在Android中以编程方式检测死锁?

时间:2017-02-15 10:15:15

标签: java android multithreading deadlock detection

我知道并使用可用资源here以Java编程方式检测死锁。

  ThreadMXBean bean = ManagementFactory.getThreadMXBean();

    long ids[] = bean.findMonitorDeadlockedThreads();

    if(ids != null)
    {
        ThreadInfo threadInfo[] = bean.getThreadInfo(ids);

        for (ThreadInfo threadInfo1 : threadInfo)
        {
            System.out.println(threadInfo1.getThreadId());    //Prints the ID of deadlocked thread

            System.out.println(threadInfo1.getThreadName());  //Prints the name of deadlocked thread

            System.out.println(threadInfo1.getLockName());    //Prints the string representation of an object for which thread has entered into deadlock.

            System.out.println(threadInfo1.getLockOwnerId());  //Prints the ID of thread which currently owns the object lock

            System.out.println(threadInfo1.getLockOwnerName());  //Prints name of the thread which currently owns the object lock.
        }
    }
    else
    {
        System.out.println("No Deadlocked Threads");
    }

上面的代码确定并打印死锁线程和相关信息。我在Android Studio中尝试过相同的操作,发现android并不支持此功能。是否有与android中相同的内容?

1 个答案:

答案 0 :(得分:2)

ThreadMXBean不是Android Framework的一部分。这个问题已经存在Android finding a deadlock