Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/FindDeadlocks.java
41152 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26* @bug 508647027* @key intermittent28* @summary Basic Test for the following methods:29* - ThreadMXBean.findDeadlockedThreads()30* - ThreadMXBean.findMonitorDeadlockedThreads()31* @author Mandy Chung32*33* @build MonitorDeadlock34* @build SynchronizerDeadlock35* @build ThreadDump36* @run main/othervm FindDeadlocks37*/3839import java.lang.management.*;40import java.util.*;4142public class FindDeadlocks {43static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();44public static void main(String[] argv) {45ThreadMXBean mbean = ManagementFactory.getThreadMXBean();46// create deadlocked threads47MonitorDeadlock md = new MonitorDeadlock();4849// no deadlock50if (findDeadlocks() != null) {51throw new RuntimeException("TEST FAILED: Should return null.");52}5354// Let the threads to proceed55md.goDeadlock();56// wait until the deadlock is ready57md.waitUntilDeadlock();5859long[] mthreads = findDeadlocks();60if (mthreads == null) {61ThreadDump.dumpStacks();62throw new RuntimeException("TEST FAILED: Deadlock not detected.");63}64md.checkResult(mthreads);6566// create deadlocked threads on synchronizers67SynchronizerDeadlock sd = new SynchronizerDeadlock();6869// Let the threads to proceed70sd.goDeadlock();71// wait until the deadlock is ready72sd.waitUntilDeadlock();7374// Find Deadlock75long[] threads = findDeadlocks();76if (threads == null) {77ThreadDump.dumpStacks();78throw new RuntimeException("TEST FAILED: Deadlock not detected.");79}8081// form a list of newly deadlocked threads82long[] newList = new long[threads.length - mthreads.length];83int count = 0;84for (int i = 0; i < threads.length; i++) {85long id = threads[i];86boolean isNew = true;87for (int j = 0; j < mthreads.length; j++) {88if (mthreads[j] == id) {89isNew = false;90break;91}92}93if (isNew) {94newList[count++] = id;95}96}9798if (mbean.isSynchronizerUsageSupported()) {99sd.checkResult(newList);100} else {101// monitoring of synchronizer usage not supported102if (count != 0) {103throw new RuntimeException("TEST FAILED: NewList should be empty.");104}105}106107// Print Deadlock stack trace108System.out.println("Found threads that are in deadlock:-");109ThreadInfo[] infos = mbean.getThreadInfo(threads, Integer.MAX_VALUE);110for (int i = 0; i < infos.length; i++) {111ThreadDump.printThreadInfo(infos[i]);112}113114System.out.println("Test passed");115}116static long[] findDeadlocks() {117long[] threads;118if (mbean.isSynchronizerUsageSupported()) {119threads = mbean.findDeadlockedThreads();120} else {121threads = mbean.findMonitorDeadlockedThreads();122}123return threads;124}125126}127128129