Get reference to Thread Object from its ID

前端 未结 2 1616
慢半拍i
慢半拍i 2020-12-31 00:02

How can I get reference to a Running Thread if I know the ID associated with that Thread?

e.g.

long threadID = 12342;
Thread thread          


        
2条回答
  •  礼貌的吻别
    2020-12-31 00:34

    You can use following code in order to get the Thread Name (For e.g. I want to get names of Threads that are in deadlock )

    ThreadMXBean threadMB = ManagementFactory.getThreadMXBean();
    long threadIds[] = threadMB.findDeadlockedThreads();
    for (long id : threadIds) {
         System.out.println("The deadLock Thread id is : " + id
                                + "  > "
                                +       
         threadMB.getThreadInfo(id).getThreadName());
    }
    

提交回复
热议问题