Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ThreadMXBean/FindDeadlocks.java
38821 views
/*1* Copyright (c) 2005, 2010, 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* @summary Basic Test for the following methods:28* - ThreadMXBean.findDeadlockedThreads()29* - ThreadMXBean.findMonitorDeadlockedThreads()30* @author Mandy Chung31*32* @build MonitorDeadlock33* @build SynchronizerDeadlock34* @build ThreadDump35* @run main/othervm FindDeadlocks36*/3738import java.lang.management.*;39import java.util.*;4041public class FindDeadlocks {42static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();43public static void main(String[] argv) {44ThreadMXBean mbean = ManagementFactory.getThreadMXBean();45// create deadlocked threads46MonitorDeadlock md = new MonitorDeadlock();4748// no deadlock49if (findDeadlocks() != null) {50throw new RuntimeException("TEST FAILED: Should return null.");51}5253// Let the threads to proceed54md.goDeadlock();55// wait until the deadlock is ready56md.waitUntilDeadlock();5758long[] mthreads = findDeadlocks();59if (mthreads == null) {60ThreadDump.dumpStacks();61throw new RuntimeException("TEST FAILED: Deadlock not detected.");62}63md.checkResult(mthreads);6465// create deadlocked threads on synchronizers66SynchronizerDeadlock sd = new SynchronizerDeadlock();6768// Let the threads to proceed69sd.goDeadlock();70// wait until the deadlock is ready71sd.waitUntilDeadlock();7273// Find Deadlock74long[] threads = findDeadlocks();75if (threads == null) {76ThreadDump.dumpStacks();77throw new RuntimeException("TEST FAILED: Deadlock not detected.");78}7980// form a list of newly deadlocked threads81long[] newList = new long[threads.length - mthreads.length];82int count = 0;83for (int i = 0; i < threads.length; i++) {84long id = threads[i];85boolean isNew = true;86for (int j = 0; j < mthreads.length; j++) {87if (mthreads[j] == id) {88isNew = false;89break;90}91}92if (isNew) {93newList[count++] = id;94}95}9697if (mbean.isSynchronizerUsageSupported()) {98sd.checkResult(newList);99} else {100// monitoring of synchronizer usage not supported101if (count != 0) {102throw new RuntimeException("TEST FAILED: NewList should be empty.");103}104}105106// Print Deadlock stack trace107System.out.println("Found threads that are in deadlock:-");108ThreadInfo[] infos = mbean.getThreadInfo(threads, Integer.MAX_VALUE);109for (int i = 0; i < infos.length; i++) {110ThreadDump.printThreadInfo(infos[i]);111}112113System.out.println("Test passed");114}115static long[] findDeadlocks() {116long[] threads;117if (mbean.isSynchronizerUsageSupported()) {118threads = mbean.findDeadlockedThreads();119} else {120threads = mbean.findMonitorDeadlockedThreads();121}122return threads;123}124125}126127128