Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/MonitorDeadlock.java
41152 views
/*1* Copyright (c) 2005, 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*/2223/*24* @summary MonitorDeadlock creates threads that are deadlocked on25* object monitors.26* @author Mandy Chung27* @build Barrier28*/2930import java.lang.management.*;31import java.util.*;3233public class MonitorDeadlock {3435private final int EXPECTED_THREADS = 3;36private Barrier go = new Barrier(1);37private Barrier barr = new Barrier(EXPECTED_THREADS);3839private Object a = new Object();40private Object b = new Object();41private Object c = new Object();42private Thread[] dThreads = new Thread[EXPECTED_THREADS];4344public MonitorDeadlock() {45dThreads[0] = new DeadlockingThread("MThread-1", a, b);46dThreads[1] = new DeadlockingThread("MThread-2", b, c);47dThreads[2] = new DeadlockingThread("MThread-3", c, a);4849// make them daemon threads so that the test will exit50for (int i = 0; i < EXPECTED_THREADS; i++) {51dThreads[i].setDaemon(true);52dThreads[i].start();53}54}5556void goDeadlock() {57// Wait until all threads have started58barr.await();5960// reset for later signals61barr.set(EXPECTED_THREADS);6263while (go.getWaiterCount() != EXPECTED_THREADS) {64synchronized(this) {65try {66wait(100);67} catch (InterruptedException e) {68// ignore69}70}71}7273// sleep a little so that all threads are blocked before notified.74try {75Thread.sleep(100);76} catch (InterruptedException e) {77// ignore78}79go.signal();8081}8283void waitUntilDeadlock() {84barr.await();8586for (int i=0; i < 100; i++) {87// sleep a little while to wait until threads are blocked.88try {89Thread.sleep(100);90} catch (InterruptedException e) {91// ignore92}93boolean retry = false;94for (Thread t: dThreads) {95if (t.getState() == Thread.State.RUNNABLE) {96retry = true;97break;98}99}100if (!retry) {101break;102}103}104}105106private class DeadlockingThread extends Thread {107private final Object lock1;108private final Object lock2;109110DeadlockingThread(String name, Object lock1, Object lock2) {111super(name);112this.lock1 = lock1;113this.lock2 = lock2;114}115public void run() {116f();117}118private void f() {119synchronized (lock1) {120barr.signal();121go.await();122g();123}124}125private void g() {126barr.signal();127synchronized (lock2) {128throw new RuntimeException("should not reach here.");129}130}131}132133void checkResult(long[] threads) {134if (threads.length != EXPECTED_THREADS) {135throw new RuntimeException("Expected to have " +136EXPECTED_THREADS + " to be in the deadlock list");137}138boolean[] found = new boolean[EXPECTED_THREADS];139for (int i = 0; i < threads.length; i++) {140for (int j = 0; j < dThreads.length; j++) {141if (dThreads[j].getId() == threads[i]) {142found[j] = true;143}144}145}146boolean ok = true;147for (int j = 0; j < found.length; j++) {148ok = ok && found[j];149}150151if (!ok) {152System.out.print("Returned result is [");153for (int j = 0; j < threads.length; j++) {154System.out.print(threads[j] + " ");155}156System.out.println("]");157158System.out.print("Expected result is [");159for (int j = 0; j < threads.length; j++) {160System.out.print(dThreads[j] + " ");161}162System.out.println("]");163throw new RuntimeException("Unexpected result returned " +164" by findMonitorDeadlockedThreads method.");165}166}167}168169170