FindDeadlocks.java revision 11884:b45c81ca8671
1184610Salfred/*
2184610Salfred * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3184610Salfred * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4184610Salfred *
5189002Sed * This code is free software; you can redistribute it and/or modify it
6184610Salfred * under the terms of the GNU General Public License version 2 only, as
7184610Salfred * published by the Free Software Foundation.
8184610Salfred *
9184610Salfred * This code is distributed in the hope that it will be useful, but WITHOUT
10184610Salfred * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11184610Salfred * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12184610Salfred * version 2 for more details (a copy is included in the LICENSE file that
13184610Salfred * accompanied this code).
14184610Salfred *
15184610Salfred * You should have received a copy of the GNU General Public License version
16184610Salfred * 2 along with this work; if not, write to the Free Software Foundation,
17184610Salfred * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18184610Salfred *
19184610Salfred * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20184610Salfred * or visit www.oracle.com if you need additional information or have any
21184610Salfred * questions.
22184610Salfred */
23184610Salfred
24184610Salfred
25184610Salfred/*
26184610Salfred * @test
27184610Salfred * @bug     5086470
28184610Salfred * @key intermittent
29184610Salfred * @summary Basic Test for the following methods:
30194677Sthompsa *          - ThreadMXBean.findDeadlockedThreads()
31194677Sthompsa *          - ThreadMXBean.findMonitorDeadlockedThreads()
32194677Sthompsa * @author  Mandy Chung
33194677Sthompsa *
34194677Sthompsa * @modules java.management
35194677Sthompsa * @build MonitorDeadlock
36194677Sthompsa * @build SynchronizerDeadlock
37194677Sthompsa * @build ThreadDump
38194677Sthompsa * @run main/othervm FindDeadlocks
39194677Sthompsa */
40194677Sthompsa
41194677Sthompsaimport java.lang.management.*;
42194677Sthompsaimport java.util.*;
43194677Sthompsa
44194677Sthompsapublic class FindDeadlocks {
45194677Sthompsa    static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
46194677Sthompsa    public static void main(String[] argv) {
47194677Sthompsa        ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
48194677Sthompsa        // create deadlocked threads
49188942Sthompsa        MonitorDeadlock md = new MonitorDeadlock();
50194677Sthompsa
51184610Salfred        // no deadlock
52188942Sthompsa        if (findDeadlocks() != null) {
53188942Sthompsa            throw new RuntimeException("TEST FAILED: Should return null.");
54188942Sthompsa        }
55188942Sthompsa
56184610Salfred        // Let the threads to proceed
57188942Sthompsa        md.goDeadlock();
58188942Sthompsa        // wait until the deadlock is ready
59188942Sthompsa        md.waitUntilDeadlock();
60184610Salfred
61184610Salfred        long[] mthreads = findDeadlocks();
62184610Salfred        if (mthreads == null) {
63184610Salfred            ThreadDump.dumpStacks();
64184610Salfred            throw new RuntimeException("TEST FAILED: Deadlock not detected.");
65184610Salfred        }
66184610Salfred        md.checkResult(mthreads);
67184610Salfred
68184610Salfred        // create deadlocked threads on synchronizers
69184610Salfred        SynchronizerDeadlock sd = new SynchronizerDeadlock();
70184610Salfred
71184610Salfred        // Let the threads to proceed
72228483Shselasky        sd.goDeadlock();
73228483Shselasky        // wait until the deadlock is ready
74228483Shselasky        sd.waitUntilDeadlock();
75184610Salfred
76227843Smarius        // Find Deadlock
77184610Salfred        long[] threads = findDeadlocks();
78184610Salfred        if (threads == null) {
79184610Salfred            ThreadDump.dumpStacks();
80184610Salfred            throw new RuntimeException("TEST FAILED: Deadlock not detected.");
81184610Salfred        }
82184610Salfred
83184610Salfred        // form a list of newly deadlocked threads
84184610Salfred        long[] newList = new long[threads.length - mthreads.length];
85184610Salfred        int count = 0;
86184610Salfred        for (int i = 0; i < threads.length; i++) {
87184610Salfred            long id = threads[i];
88188942Sthompsa            boolean isNew = true;
89184610Salfred            for (int j = 0; j < mthreads.length; j++) {
90184610Salfred                if (mthreads[j] == id) {
91184610Salfred                    isNew = false;
92184610Salfred                    break;
93184610Salfred                }
94184610Salfred            }
95184610Salfred            if (isNew) {
96184610Salfred                newList[count++] = id;
97184610Salfred            }
98184610Salfred        }
99184610Salfred
100184610Salfred        if (mbean.isSynchronizerUsageSupported()) {
101184610Salfred            sd.checkResult(newList);
102184610Salfred        } else {
103184610Salfred            // monitoring of synchronizer usage not supported
104184610Salfred            if (count != 0) {
105184610Salfred                throw new RuntimeException("TEST FAILED: NewList should be empty.");
106187170Sthompsa            }
107186439Sthompsa        }
108187170Sthompsa
109187170Sthompsa        // Print Deadlock stack trace
110187170Sthompsa        System.out.println("Found threads that are in deadlock:-");
111187170Sthompsa        ThreadInfo[] infos = mbean.getThreadInfo(threads, Integer.MAX_VALUE);
112194228Sthompsa        for (int i = 0; i < infos.length; i++) {
113184610Salfred            ThreadDump.printThreadInfo(infos[i]);
114184610Salfred        }
115184610Salfred
116184610Salfred        System.out.println("Test passed");
117184610Salfred    }
118184610Salfred    static long[] findDeadlocks() {
119184610Salfred        long[] threads;
120184610Salfred        if (mbean.isSynchronizerUsageSupported()) {
121184610Salfred            threads = mbean.findDeadlockedThreads();
122184610Salfred        } else {
123184610Salfred            threads = mbean.findMonitorDeadlockedThreads();
124184610Salfred        }
125184610Salfred        return threads;
126184610Salfred    }
127184610Salfred
128184610Salfred}
129184610Salfred