本文整理匯總了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.");
}
}
}
}