Determine which thread owns a monitor

前端 未结 4 1327
有刺的猬
有刺的猬 2020-12-06 01:50

Is there a way to tell, for a Java object, which Thread (or null) currently owns its monitor? Or at least a way to tell if the current thread owns it?

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 02:45

    In Java 1.6 you can use Reflection to get this information.

    ThreadMXBean tBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threadInfo = tBean .getThreadInfo(bean.getAllThreadIds(), true, true);
    

提交回复
热议问题