blob: 51f66269b0e4efe36955c220c41b95d530fd249d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
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
39import java.lang.management.*;
40import java.util.*;
41
42public 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}