2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
|
|
25 |
/*
|
|
26 |
* @test
|
|
27 |
* @bug 5086470
|
|
28 |
* @summary Basic Test for the following methods:
|
|
29 |
* - ThreadMXBean.findDeadlockedThreads()
|
|
30 |
* - ThreadMXBean.findMonitorDeadlockedThreads()
|
|
31 |
* @author Mandy Chung
|
|
32 |
*
|
|
33 |
* @build MonitorDeadlock
|
|
34 |
* @build SynchronizerDeadlock
|
|
35 |
* @build ThreadDump
|
|
36 |
* @run main FindDeadlocks
|
|
37 |
*/
|
|
38 |
|
|
39 |
import java.lang.management.*;
|
|
40 |
import java.util.*;
|
|
41 |
|
|
42 |
public class FindDeadlocks {
|
|
43 |
static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
|
|
44 |
public static void main(String[] argv) {
|
|
45 |
ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
|
|
46 |
// create deadlocked threads
|
|
47 |
MonitorDeadlock md = new MonitorDeadlock();
|
|
48 |
|
|
49 |
// no deadlock
|
|
50 |
if (findDeadlocks() != null) {
|
|
51 |
throw new RuntimeException("TEST FAILED: Should return null.");
|
|
52 |
}
|
|
53 |
|
|
54 |
// Let the threads to proceed
|
|
55 |
md.goDeadlock();
|
|
56 |
// wait until the deadlock is ready
|
|
57 |
md.waitUntilDeadlock();
|
|
58 |
|
|
59 |
long[] mthreads = findDeadlocks();
|
|
60 |
if (mthreads == null) {
|
|
61 |
ThreadDump.dumpStacks();
|
|
62 |
throw new RuntimeException("TEST FAILED: Deadlock not detected.");
|
|
63 |
}
|
|
64 |
md.checkResult(mthreads);
|
|
65 |
|
|
66 |
// create deadlocked threads on synchronizers
|
|
67 |
SynchronizerDeadlock sd = new SynchronizerDeadlock();
|
|
68 |
|
|
69 |
// Let the threads to proceed
|
|
70 |
sd.goDeadlock();
|
|
71 |
// wait until the deadlock is ready
|
|
72 |
sd.waitUntilDeadlock();
|
|
73 |
|
|
74 |
// Find Deadlock
|
|
75 |
long[] threads = findDeadlocks();
|
|
76 |
if (threads == null) {
|
|
77 |
ThreadDump.dumpStacks();
|
|
78 |
throw new RuntimeException("TEST FAILED: Deadlock not detected.");
|
|
79 |
}
|
|
80 |
|
|
81 |
// form a list of newly deadlocked threads
|
|
82 |
long[] newList = new long[threads.length - mthreads.length];
|
|
83 |
int count = 0;
|
|
84 |
for (int i = 0; i < threads.length; i++) {
|
|
85 |
long id = threads[i];
|
|
86 |
boolean isNew = true;
|
|
87 |
for (int j = 0; j < mthreads.length; j++) {
|
|
88 |
if (mthreads[j] == id) {
|
|
89 |
isNew = false;
|
|
90 |
break;
|
|
91 |
}
|
|
92 |
}
|
|
93 |
if (isNew) {
|
|
94 |
newList[count++] = id;
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
|
98 |
if (mbean.isSynchronizerUsageSupported()) {
|
|
99 |
sd.checkResult(newList);
|
|
100 |
} else {
|
|
101 |
// monitoring of synchronizer usage not supported
|
|
102 |
if (count != 0) {
|
|
103 |
throw new RuntimeException("TEST FAILED: NewList should be empty.");
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
// Print Deadlock stack trace
|
|
108 |
System.out.println("Found threads that are in deadlock:-");
|
|
109 |
ThreadInfo[] infos = mbean.getThreadInfo(threads, Integer.MAX_VALUE);
|
|
110 |
for (int i = 0; i < infos.length; i++) {
|
|
111 |
ThreadDump.printThreadInfo(infos[i]);
|
|
112 |
}
|
|
113 |
|
|
114 |
System.out.println("Test passed");
|
|
115 |
}
|
|
116 |
static long[] findDeadlocks() {
|
|
117 |
long[] threads;
|
|
118 |
if (mbean.isSynchronizerUsageSupported()) {
|
|
119 |
threads = mbean.findDeadlockedThreads();
|
|
120 |
} else {
|
|
121 |
threads = mbean.findMonitorDeadlockedThreads();
|
|
122 |
}
|
|
123 |
return threads;
|
|
124 |
}
|
|
125 |
|
|
126 |
}
|