Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/management/FullThreadDump/Deadlock.java
38829 views
/*1* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940/*41*/4243import java.util.concurrent.CyclicBarrier;44import java.util.concurrent.BrokenBarrierException;45import java.util.concurrent.locks.Lock;46import java.util.concurrent.locks.ReentrantLock;47import java.io.IOException;4849/**50* This Deadlock class demonstrates the capability of performing51* deadlock detection programmatically within the application using52* the java.lang.management API.53*54* See ThreadMonitor.java for the use of java.lang.management.ThreadMXBean55* API.56*/57public class Deadlock {58public static void main(String[] argv) {59new Deadlock();6061// Now find deadlock62ThreadMonitor monitor = new ThreadMonitor();63boolean found = false;64while (!found) {65found = monitor.findDeadlock();66try {67Thread.sleep(500);68} catch (InterruptedException e) {69System.exit(1);70}71}7273System.out.println("\nPress <Enter> to exit this Deadlock program.\n");74waitForEnterPressed();75}767778private CyclicBarrier barrier = new CyclicBarrier(6);79public Deadlock() {80DeadlockThread[] dThreads = new DeadlockThread[6];8182Monitor a = new Monitor("a");83Monitor b = new Monitor("b");84Monitor c = new Monitor("c");85dThreads[0] = new DeadlockThread("MThread-1", a, b);86dThreads[1] = new DeadlockThread("MThread-2", b, c);87dThreads[2] = new DeadlockThread("MThread-3", c, a);8889Lock d = new ReentrantLock();90Lock e = new ReentrantLock();91Lock f = new ReentrantLock();9293dThreads[3] = new DeadlockThread("SThread-4", d, e);94dThreads[4] = new DeadlockThread("SThread-5", e, f);95dThreads[5] = new DeadlockThread("SThread-6", f, d);9697// make them daemon threads so that the test will exit98for (int i = 0; i < 6; i++) {99dThreads[i].setDaemon(true);100dThreads[i].start();101}102}103104class DeadlockThread extends Thread {105private Lock lock1 = null;106private Lock lock2 = null;107private Monitor mon1 = null;108private Monitor mon2 = null;109private boolean useSync;110111DeadlockThread(String name, Lock lock1, Lock lock2) {112super(name);113this.lock1 = lock1;114this.lock2 = lock2;115this.useSync = true;116}117DeadlockThread(String name, Monitor mon1, Monitor mon2) {118super(name);119this.mon1 = mon1;120this.mon2 = mon2;121this.useSync = false;122}123@Override124public void run() {125if (useSync) {126syncLock();127} else {128monitorLock();129}130}131private void syncLock() {132lock1.lock();133try {134try {135barrier.await();136} catch (InterruptedException e) {137e.printStackTrace();138System.exit(1);139} catch (BrokenBarrierException e) {140e.printStackTrace();141System.exit(1);142}143goSyncDeadlock();144} finally {145lock1.unlock();146}147}148private void goSyncDeadlock() {149try {150barrier.await();151} catch (InterruptedException e) {152e.printStackTrace();153System.exit(1);154} catch (BrokenBarrierException e) {155e.printStackTrace();156System.exit(1);157}158lock2.lock();159throw new RuntimeException("should not reach here.");160}161private void monitorLock() {162synchronized (mon1) {163try {164barrier.await();165} catch (InterruptedException e) {166e.printStackTrace();167System.exit(1);168} catch (BrokenBarrierException e) {169e.printStackTrace();170System.exit(1);171}172goMonitorDeadlock();173}174}175private void goMonitorDeadlock() {176try {177barrier.await();178} catch (InterruptedException e) {179e.printStackTrace();180System.exit(1);181} catch (BrokenBarrierException e) {182e.printStackTrace();183System.exit(1);184}185synchronized (mon2) {186throw new RuntimeException(getName() + " should not reach here.");187}188}189}190191class Monitor {192String name;193Monitor(String name) {194this.name = name;195}196}197198private static void waitForEnterPressed() {199try {200boolean done = false;201while (!done) {202char ch = (char) System.in.read();203if (ch<0||ch=='\n') {204done = true;205}206}207}208catch (IOException e) {209e.printStackTrace();210System.exit(0);211}212}213}214215216