本文整理汇总了Java中java.lang.management.ThreadMXBean.findMonitorDeadlockedThreads方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadMXBean.findMonitorDeadlockedThreads方法的具体用法?Java ThreadMXBean.findMonitorDeadlockedThreads怎么用?Java ThreadMXBean.findMonitorDeadlockedThreads使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.management.ThreadMXBean
的用法示例。
在下文中一共展示了ThreadMXBean.findMonitorDeadlockedThreads方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildDeadlockInfo
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
static String buildDeadlockInfo() {
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long[] threadIds = threadBean.findMonitorDeadlockedThreads();
if (threadIds != null && threadIds.length > 0) {
StringWriter stringWriter = new StringWriter();
PrintWriter out = new PrintWriter(stringWriter);
ThreadInfo[] infos = threadBean.getThreadInfo(threadIds, true, true);
for (ThreadInfo ti : infos) {
printThreadInfo(ti, out);
printLockInfo(ti.getLockedSynchronizers(), out);
out.println();
}
out.close();
return stringWriter.toString();
} else {
return null;
}
}
示例2: findDeadlockedThreads
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
private long[] findDeadlockedThreads(ThreadMXBean mbean)
{
// JDK 1.5 only supports the findMonitorDeadlockedThreads()
// method, so you need to comment out the following three lines
if (mbean.isSynchronizerUsageSupported())
{
return mbean.findDeadlockedThreads();
}
return mbean.findMonitorDeadlockedThreads();
}
示例3: main
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
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());
}
}
}
示例4: testBug70704
import java.lang.management.ThreadMXBean; //导入方法依赖的package包/类
/**
* Tests fix for Bug#70704 - Deadlock using UpdatableResultSet.
*
* Doesn't actually test the buggy behavior since it is not verifiable since the fix for Bug#59462 (revision 385a151). However, the patch for this fix is
* needed because the synchronization in UpdatableResultSet was dated.
* This test makes sure there is no regression.
*
* WARNING! If this test fails there is no guarantee that the JVM will remain stable and won't affect any other tests. It is imperative that this test
* passes to ensure other tests results.
*/
public void testBug70704() throws Exception {
for (int i = 0; i < 100; i++) {
final Statement testStmt = this.conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
final ResultSet testRs = testStmt.executeQuery("SELECT 1");
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(new Callable<Void>() {
public Void call() throws Exception {
testStmt.close();
return null;
}
});
executorService.submit(new Callable<Void>() {
public Void call() throws Exception {
testRs.close();
return null;
}
});
executorService.shutdown();
if (!executorService.awaitTermination(2, TimeUnit.SECONDS)) {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
long[] threadIds = threadMXBean.findMonitorDeadlockedThreads();
if (threadIds != null) {
System.err.println("Deadlock detected!");
ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadIds, Integer.MAX_VALUE);
for (ThreadInfo ti : threadInfos) {
System.err.println();
System.err.println(ti);
System.err.println("Stack trace:");
for (StackTraceElement ste : ti.getStackTrace()) {
System.err.println(" " + ste);
}
}
fail("Unexpected deadlock detected. Consult system output for more details. WARNING: this failure may lead to JVM instability.");
}
}
}
}