Java Code Examples for java.lang.management.ThreadMXBean#findDeadlockedThreads()

The following examples show how to use java.lang.management.ThreadMXBean#findDeadlockedThreads() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SharedSynchronizer.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        MyThread t = new MyThread();
        t.setDaemon(true);
        t.start();

        ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
        if (!tmbean.isSynchronizerUsageSupported()) {
            System.out.println("Monitoring of synchronizer usage not supported")
;
            return;
        }

        long[] result = tmbean.findDeadlockedThreads();
        if (result != null) {
             throw new RuntimeException("TEST FAILED: result should be null");
        }
    }
 
Example 2
Source File: SharedSynchronizer.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        MyThread t = new MyThread();
        t.setDaemon(true);
        t.start();

        ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
        if (!tmbean.isSynchronizerUsageSupported()) {
            System.out.println("Monitoring of synchronizer usage not supported")
;
            return;
        }

        long[] result = tmbean.findDeadlockedThreads();
        if (result != null) {
             throw new RuntimeException("TEST FAILED: result should be null");
        }
    }
 
Example 3
Source File: DebugUtils.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns deadlocked threads stack trace.
 *
 * @return deadlocked threads stack trace
 */
@NotNull
public static String getDeadlockStackTrace ()
{
    final ThreadMXBean bean = ManagementFactory.getThreadMXBean ();
    final long[] threadIds = bean.findDeadlockedThreads ();
    final StringBuilder stackTrace = new StringBuilder ();
    if ( threadIds != null )
    {
        final ThreadInfo[] infos = bean.getThreadInfo ( threadIds );
        for ( final ThreadInfo info : infos )
        {
            final StackTraceElement[] stack = info.getStackTrace ();
            stackTrace.append ( ExceptionUtils.getStackTrace ( stack ) );
            stackTrace.append ( info != infos[ infos.length - 1 ] ? "\n" : "" );
        }
    }
    return stackTrace.toString ();
}
 
Example 4
Source File: SharedSynchronizer.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        MyThread t = new MyThread();
        t.setDaemon(true);
        t.start();

        ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
        if (!tmbean.isSynchronizerUsageSupported()) {
            System.out.println("Monitoring of synchronizer usage not supported")
;
            return;
        }

        long[] result = tmbean.findDeadlockedThreads();
        if (result != null) {
             throw new RuntimeException("TEST FAILED: result should be null");
        }
    }
 
Example 5
Source File: SharedSynchronizer.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        MyThread t = new MyThread();
        t.setDaemon(true);
        t.start();

        ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
        if (!tmbean.isSynchronizerUsageSupported()) {
            System.out.println("Monitoring of synchronizer usage not supported")
;
            return;
        }

        long[] result = tmbean.findDeadlockedThreads();
        if (result != null) {
             throw new RuntimeException("TEST FAILED: result should be null");
        }
    }
 
Example 6
Source File: SharedSynchronizer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        MyThread t = new MyThread();
        t.setDaemon(true);
        t.start();

        ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
        if (!tmbean.isSynchronizerUsageSupported()) {
            System.out.println("Monitoring of synchronizer usage not supported")
;
            return;
        }

        long[] result = tmbean.findDeadlockedThreads();
        if (result != null) {
             throw new RuntimeException("TEST FAILED: result should be null");
        }
    }
 
Example 7
Source File: SharedSynchronizer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        MyThread t = new MyThread();
        t.setDaemon(true);
        t.start();

        ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
        if (!tmbean.isSynchronizerUsageSupported()) {
            System.out.println("Monitoring of synchronizer usage not supported")
;
            return;
        }

        long[] result = tmbean.findDeadlockedThreads();
        if (result != null) {
             throw new RuntimeException("TEST FAILED: result should be null");
        }
    }
 
Example 8
Source File: TestDeadlock.java    From advanced_java_concurrency with MIT License 6 votes vote down vote up
@Test
public void testDeadlock() throws InterruptedException {
	new Thread(() -> OperationsDeadlock.main(null)).start();

	// Sleeping for deadlock to happen
	Thread.sleep(1000);

	// Programmatic way to detect deadlock

	ThreadMXBean bean = ManagementFactory.getThreadMXBean();
	long[] threadIds = bean.findDeadlockedThreads(); // Returns null if no
														// threads are
														// deadlocked.

	Assert.assertNotNull(threadIds);
	Assert.assertEquals(2, threadIds.length);

	ThreadInfo[] infos = bean.getThreadInfo(threadIds);

	for (ThreadInfo info : infos) {
		System.out.println(info);
	}
}
 
Example 9
Source File: SharedSynchronizer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        MyThread t = new MyThread();
        t.setDaemon(true);
        t.start();

        ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
        if (!tmbean.isSynchronizerUsageSupported()) {
            System.out.println("Monitoring of synchronizer usage not supported")
;
            return;
        }

        long[] result = tmbean.findDeadlockedThreads();
        if (result != null) {
             throw new RuntimeException("TEST FAILED: result should be null");
        }
    }
 
Example 10
Source File: DeadLockDetector.java    From cst with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void run() {
	do {
		ThreadMXBean tmx = ManagementFactory.getThreadMXBean();
		long[] ids = tmx.findDeadlockedThreads();
		if (ids != null) {
			ThreadInfo[] infos = tmx.getThreadInfo(ids, true, true);
			System.out.println("The following threads are deadlocked: ");
			for (ThreadInfo ti : infos) {
				System.out.println(ti);
			}
		}
		try {
			Thread.currentThread().sleep(this.ddRefreshPeriod);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	} while (shouldLoop);
}
 
Example 11
Source File: SharedSynchronizer.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        MyThread t = new MyThread();
        t.setDaemon(true);
        t.start();

        ThreadMXBean tmbean = ManagementFactory.getThreadMXBean();
        if (!tmbean.isSynchronizerUsageSupported()) {
            System.out.println("Monitoring of synchronizer usage not supported")
;
            return;
        }

        long[] result = tmbean.findDeadlockedThreads();
        if (result != null) {
             throw new RuntimeException("TEST FAILED: result should be null");
        }
    }
 
Example 12
Source File: ThreadInfoTask.java    From bistoury with GNU General Public License v3.0 6 votes vote down vote up
private ThreadInfo[] dump(VirtualMachineUtil.VMConnector connect, int maxDepth, boolean onlyDeadLock) {
    try {
        ThreadMXBean threadBean = connect.getThreadMXBean();
        long[] ids;
        if (onlyDeadLock) {
            ids = threadBean.findDeadlockedThreads();
        } else {
            ids = threadBean.getAllThreadIds();
        }
        if (ids != null) {
            return threadBean.getThreadInfo(ids, maxDepth);
        } else {
            return new ThreadInfo[]{};
        }
    } catch (IOException e) {
        logger.error("dump thread error", e);
        return new ThreadInfo[]{};
    }
}
 
Example 13
Source File: ThreadStatus.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public void run() {
	try {
		ThreadMXBean bean = ManagementFactory.getThreadMXBean();
		for (int i = 0; i < repeat; i++) {
			List<String> list = new ArrayList<>();
			int deadLockedCount = bean.findDeadlockedThreads() == null ? 0 : bean.findDeadlockedThreads().length;
			list.add(String.format("thread total started:%d, count:%d, peak:%d, daemon:%d, dead:%d.",
					bean.getTotalStartedThreadCount(), bean.getThreadCount(), bean.getPeakThreadCount(),
					bean.getDaemonThreadCount(), deadLockedCount));
			if (BooleanUtils.isTrue(Servers.centerServerIsRunning())) {
				list.add(String.format("  +++ center server thread pool size:%d, idle:%d.",
						Servers.centerServer.getThreadPool().getThreads(),
						Servers.centerServer.getThreadPool().getIdleThreads()));
			}
			if (BooleanUtils.isTrue(Servers.applicationServerIsRunning())) {
				list.add(String.format("  +++ application server thread pool size:%d, idle:%d.",
						Servers.applicationServer.getThreadPool().getThreads(),
						Servers.applicationServer.getThreadPool().getIdleThreads()));
			}
			if (BooleanUtils.isTrue(Servers.webServerIsRunning())) {
				list.add(String.format("  +++ web server thread pool size:%d, idle:%d.",
						Servers.webServer.getThreadPool().getThreads(),
						Servers.webServer.getThreadPool().getIdleThreads()));
			}
			System.out.println(StringUtils.join(list, StringUtils.LF));
			Thread.sleep(2000);
		}
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}
 
Example 14
Source File: Test.java    From ClusterDeviceControlPlatform with MIT License 5 votes vote down vote up
private static void printThreadInfo() {
    ThreadMXBean thread = ManagementFactory.getThreadMXBean();
    System.out.println("ObjectName=" + thread.getObjectName());
    System.out.println("仍活动的线程总数=" + thread.getThreadCount());
    System.out.println("峰值=" + thread.getPeakThreadCount());
    System.out.println("线程总数(被创建并执行过的线程总数)=" + thread.getTotalStartedThreadCount());
    System.out.println("当初仍活动的守护线程(daemonThread)总数=" + thread.getDaemonThreadCount());

    //检查是否有死锁的线程存在
    long[] deadlockedIds = thread.findDeadlockedThreads();
    if (deadlockedIds != null && deadlockedIds.length > 0) {
        ThreadInfo[] deadlockInfos = thread.getThreadInfo(deadlockedIds);
        System.out.println("死锁线程信息:");
        System.out.println("\t\t线程名称\t\t状态\t\t");
        for (ThreadInfo deadlockInfo : deadlockInfos) {
            System.out.println("\t\t" + deadlockInfo.getThreadName() + "\t\t" + deadlockInfo.getThreadState()
                    + "\t\t" + deadlockInfo.getBlockedTime() + "\t\t" + deadlockInfo.getWaitedTime()
                    + "\t\t" + deadlockInfo.getStackTrace().toString());
        }
    }
    long[] threadIds = thread.getAllThreadIds();
    if (threadIds != null && threadIds.length > 0) {
        ThreadInfo[] threadInfos = thread.getThreadInfo(threadIds);
        System.out.println("所有线程信息:");
        System.out.println("\t\t线程名称\t\t\t\t\t状态\t\t\t\t\t线程id");
        for (ThreadInfo threadInfo : threadInfos) {
            System.out.println("\t\t" + threadInfo.getThreadName() + "\t\t\t\t\t" + threadInfo.getThreadState()
                    + "\t\t\t\t\t" + threadInfo.getThreadId());
        }
    }

}
 
Example 15
Source File: JavaInformations.java    From javamelody with Apache License 2.0 5 votes vote down vote up
private static long[] getDeadlockedThreads(ThreadMXBean threadBean) {
	final long[] deadlockedThreads;
	if (threadBean.isSynchronizerUsageSupported()) {
		deadlockedThreads = threadBean.findDeadlockedThreads();
	} else {
		deadlockedThreads = threadBean.findMonitorDeadlockedThreads();
	}
	if (deadlockedThreads != null) {
		Arrays.sort(deadlockedThreads);
	}
	return deadlockedThreads;
}
 
Example 16
Source File: ProxyClient.java    From jvmtop with GNU General Public License v2.0 5 votes vote down vote up
public long[] findDeadlockedThreads() throws IOException {
    ThreadMXBean tm = getThreadMXBean();
    if (supportsLockUsage && tm.isSynchronizerUsageSupported()) {
        return tm.findDeadlockedThreads();
    } else {
        return tm.findMonitorDeadlockedThreads();
    }
}
 
Example 17
Source File: GfxdMemberMBeanBridge.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public String detectDeadlocksAlt() {
  ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
  long[] findDeadlockedThreads = threadMXBean.findDeadlockedThreads();
  StringBuilder builder = new StringBuilder();

  if (findDeadlockedThreads == null || findDeadlockedThreads.length == 0) {
    builder.append("No deadlocks detected.");
  } else {
    ThreadInfo[] threadsInfo = threadMXBean.getThreadInfo(findDeadlockedThreads);
    for (ThreadInfo threadInfo : threadsInfo) {
      builder.append(threadInfo.toString()).append(ManagementUtils.LINE_SEPARATOR);
    }
  }
  return builder.toString();
}
 
Example 18
Source File: GfxdMemberMBeanBridge.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public String detectDeadlocksAlt() {
  ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
  long[] findDeadlockedThreads = threadMXBean.findDeadlockedThreads();
  StringBuilder builder = new StringBuilder();

  if (findDeadlockedThreads == null || findDeadlockedThreads.length == 0) {
    builder.append("No deadlocks detected.");
  } else {
    ThreadInfo[] threadsInfo = threadMXBean.getThreadInfo(findDeadlockedThreads);
    for (ThreadInfo threadInfo : threadsInfo) {
      builder.append(threadInfo.toString()).append(ManagementUtils.LINE_SEPARATOR);
    }
  }
  return builder.toString();
}
 
Example 19
Source File: ThreadDetails.java    From nifi with Apache License 2.0 4 votes vote down vote up
private ThreadDetails(final ThreadMXBean mbean) {
    threadInfos = mbean.dumpAllThreads(true, true);
    deadlockedThreadIds = mbean.findDeadlockedThreads();
    monitorDeadlockThreadIds = mbean.findMonitorDeadlockedThreads();
}
 
Example 20
Source File: VisorThreadDumpTask.java    From ignite with Apache License 2.0 3 votes vote down vote up
/** {@inheritDoc} */
@Override protected VisorThreadDumpTaskResult run(Void arg) {
    ThreadMXBean mx = U.getThreadMx();

    ThreadInfo[] info = mx.dumpAllThreads(true, true);

    VisorThreadInfo[] visorInfo = new VisorThreadInfo[info.length];

    for (int i = 0; i < info.length; i++)
        visorInfo[i] = new VisorThreadInfo(info[i]);

    return new VisorThreadDumpTaskResult(visorInfo, mx.findDeadlockedThreads());
}