use of java.lang.management.ThreadInfo in project processdash by dtuma.
the class ThreadMonitor method findDeadlock.
/**
* Checks if any threads are deadlocked. If any, print
* the thread dump information.
*/
public boolean findDeadlock() {
long[] tids = tmbean.findMonitorDeadlockedThreads();
if (tids == null) {
return false;
} else {
System.out.println("Deadlock found :-");
ThreadInfo[] tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
for (ThreadInfo ti : tinfos) {
printThreadInfo(ti);
}
return true;
}
}
use of java.lang.management.ThreadInfo in project jdk8u_jdk by JetBrains.
the class AnnotationTypeDeadlockTest method main.
public static void main(String[] args) throws Exception {
CountDownLatch prepareLatch = new CountDownLatch(2);
AtomicInteger goLatch = new AtomicInteger(1);
Task taskA = new Task(prepareLatch, goLatch, AnnA.class);
Task taskB = new Task(prepareLatch, goLatch, AnnB.class);
taskA.start();
taskB.start();
// wait until both threads start-up
prepareLatch.await();
// let them go
goLatch.set(0);
// obtain ThreadMXBean
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
// wait for threads to finish or dead-lock
while (taskA.isAlive() || taskB.isAlive()) {
// attempt to join threads
taskA.join(500L);
taskB.join(500L);
// detect dead-lock
long[] deadlockedIds = threadBean.findMonitorDeadlockedThreads();
if (deadlockedIds != null && deadlockedIds.length > 0) {
StringBuilder sb = new StringBuilder("deadlock detected:\n\n");
for (ThreadInfo ti : threadBean.getThreadInfo(deadlockedIds, Integer.MAX_VALUE)) {
sb.append(ti);
}
throw new IllegalStateException(sb.toString());
}
}
}
use of java.lang.management.ThreadInfo in project geode by apache.
the class DeadlockDetector method collectFromDependencyMonitor.
private static Set<Dependency> collectFromDependencyMonitor(ThreadMXBean bean, Serializable locality, Map<Long, ThreadInfo> threadInfos) {
HashSet<Dependency> results = new HashSet<Dependency>();
// Convert the held resources into serializable dependencies
Set<Dependency<Serializable, Thread>> heldResources = DependencyMonitorManager.getHeldResources();
for (Dependency<Serializable, Thread> dep : heldResources) {
Thread thread = dep.getDependsOn();
Serializable resource = dep.getDepender();
ThreadInfo info = threadInfos.get(thread.getId());
if (info == null) {
info = bean.getThreadInfo(thread.getId());
}
if (info != null) {
results.add(new Dependency(resource, new LocalThread(locality, info)));
}
}
Set<Dependency<Thread, Serializable>> blockedThreads = DependencyMonitorManager.getBlockedThreads();
// Convert the blocked threads into serializable dependencies
for (Dependency<Thread, Serializable> dep : blockedThreads) {
Thread thread = dep.getDepender();
ThreadInfo info = threadInfos.get(thread.getId());
if (info == null) {
info = bean.getThreadInfo(thread.getId());
}
final Serializable resource = dep.getDependsOn();
results.add(new Dependency(new LocalThread(locality, info), resource));
}
return results;
}
use of java.lang.management.ThreadInfo in project geode by apache.
the class DeadlockDetector method collectAllDependencies.
/**
* Collect all of the dependencies that exist between threads in this VM, using java management
* beans and the {@link DependencyMonitor}.
*
* Threads may depend on locks, or on other resources that are tracked by the
* {@link DependencyMonitor}.
*
* @param locality a name tag to stick on entities to help associate them with this JVM and
* distinguish them from entities from other jvms
*
* @return All of the dependencies between threads and locks or other resources on this VM.
*/
public static Set<Dependency> collectAllDependencies(Serializable locality) {
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] infos = bean.dumpAllThreads(true, true);
Set<Dependency> results = new HashSet<Dependency>();
Map<Long, ThreadInfo> threadInfos = new HashMap<Long, ThreadInfo>();
for (ThreadInfo info : infos) {
// This can happen if the thread died.
if (info == null) {
continue;
}
for (LockInfo monitor : info.getLockedMonitors()) {
Dependency dependency = new Dependency(new LocalLockInfo(locality, monitor), new LocalThread(locality, info));
results.add(dependency);
}
for (LockInfo sync : info.getLockedSynchronizers()) {
Dependency dependency = new Dependency(new LocalLockInfo(locality, sync), new LocalThread(locality, info));
results.add(dependency);
}
LockInfo waitingFor = info.getLockInfo();
if (waitingFor != null) {
Dependency dependency = new Dependency(new LocalThread(locality, info), new LocalLockInfo(locality, waitingFor));
results.add(dependency);
}
threadInfos.put(info.getThreadId(), info);
}
Set<Dependency> monitoredDependencies = collectFromDependencyMonitor(bean, locality, threadInfos);
results.addAll(monitoredDependencies);
return results;
}
use of java.lang.management.ThreadInfo in project geode by apache.
the class VMStats50 method refreshThreads.
private void refreshThreads() {
if (!THREAD_STATS_ENABLED) {
return;
}
if (this.allThreadIds == null || newThreadsStarted()) {
this.allThreadIds = threadBean.getAllThreadIds();
this.threadStartCount = threadBean.getTotalStartedThreadCount();
}
ThreadInfo[] threadInfos = threadBean.getThreadInfo(this.allThreadIds, 0);
for (int i = 0; i < threadInfos.length; i++) {
long id = this.allThreadIds[i];
ThreadInfo item = threadInfos[i];
if (item != null) {
ThreadStatInfo tsi = threadMap.get(id);
if (tsi == null) {
threadMap.put(id, new ThreadStatInfo(item, this.f.createStatistics(threadType, item.getThreadName() + '-' + item.getThreadId(), this.id)));
} else {
tsi.ti = item;
}
} else {
ThreadStatInfo tsi = threadMap.remove(id);
if (tsi != null) {
tsi.s.close();
}
}
}
Iterator<Map.Entry<Long, ThreadStatInfo>> it = threadMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Long, ThreadStatInfo> me = it.next();
long id = me.getKey();
ThreadStatInfo tsi = me.getValue();
ThreadInfo ti = tsi.ti;
Statistics s = tsi.s;
s.setLong(thread_blockedId, ti.getBlockedCount());
s.setLong(thread_lockOwnerId, ti.getLockOwnerId());
s.setLong(thread_waitedId, ti.getWaitedCount());
s.setInt(thread_inNativeId, ti.isInNative() ? 1 : 0);
s.setInt(thread_suspendedId, ti.isSuspended() ? 1 : 0);
if (threadBean.isThreadContentionMonitoringSupported() && threadBean.isThreadContentionMonitoringEnabled()) {
s.setLong(thread_blockedTimeId, ti.getBlockedTime());
s.setLong(thread_waitedTimeId, ti.getWaitedTime());
}
if (threadBean.isThreadCpuTimeSupported() && threadBean.isThreadCpuTimeEnabled()) {
s.setLong(thread_cpuTimeId, threadBean.getThreadCpuTime(id));
s.setLong(thread_userTimeId, threadBean.getThreadUserTime(id));
}
}
}
Aggregations