Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/standardmbean/DeadlockTest.java
38840 views
/*1* Copyright (c) 2005, 2014, 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* @test25* @bug 633174626* @summary Test a deadlock and will be blocked forever if the deadlock is present.27* @author Shanliang JIANG28* @run main DeadlockTest29*/3031import javax.management.*;32import javax.management.timer.*;3334public class DeadlockTest extends StandardMBean {35public <T> DeadlockTest(T implementation, Class<T> mbeanInterface)36throws NotCompliantMBeanException {37super(implementation, mbeanInterface);38}3940public MBeanInfo getCachedMBeanInfo() {41return super.getCachedMBeanInfo();42}4344public void cacheMBeanInfo(MBeanInfo mi) {45super.cacheMBeanInfo(mi);46}4748public static void main(String[] args) throws Exception {49System.out.println("main: No deadlock please.");5051System.out.println("main: Create a BadBay to hold the lock forever.");52DeadlockTest dt = new DeadlockTest(new Timer(), TimerMBean.class);5354BadBoy bb = new BadBoy(dt);55bb.start();5657synchronized(bb) {58while(!bb.gotLock) {59bb.wait(); // if blocked here, means failing to get lock, impossible.60}61}6263System.out.println("main: The BadBay is holding the lock forever.");6465System.out.println("main: Create a WorkingBoy to see blocking ...");66WorkingBoy wb = new WorkingBoy(dt);6768synchronized(wb) {69wb.start();7071while(!wb.done) {72wb.wait(); // if blocked here, the deadlock happends73}74}7576System.out.println("main: OK, bye bye.");77}7879private static class BadBoy extends Thread {80public BadBoy(Object o) {81setDaemon(true);8283this.o = o;84}8586public void run() {87System.out.println("BadBoy-run: keep synchronization lock forever!");8889synchronized(o) {90synchronized(this) {91gotLock = true;9293this.notify();94}9596try {97Thread.sleep(10000000);98} catch (Exception e) {99// OK100}101}102}103104final Object o;105public boolean gotLock;106}107108private static class WorkingBoy extends Thread {109public WorkingBoy(DeadlockTest sm) {110setDaemon(true);111112this.sm = sm;113}114115public void run() {116try {117System.out.println("WorkingBoy-run: calling StandardMBean methods ...");118119System.out.println("WorkingBoy-run: calling setImplementation ...");120sm.setImplementation(new Timer());121122System.out.println("WorkingBoy-run: calling getImplementation ...");123sm.getImplementation();124125System.out.println("WorkingBoy-run: calling getMBeanInterface ...");126sm.getMBeanInterface();127128System.out.println("WorkingBoy-run: calling getImplementationClass ...");129sm.getImplementationClass();130131System.out.println("WorkingBoy-run: calling cacheMBeanInfo ...");132sm.cacheMBeanInfo(null);133134System.out.println("WorkingBoy-run: calling getCachedMBeanInfo ...");135sm.getCachedMBeanInfo();136137System.out.println("WorkingBoy-run: All done!");138139synchronized(this) {140done = true;141142this.notifyAll();143}144} catch (NotCompliantMBeanException ne) {145// Impossible?146throw new RuntimeException(ne);147}148}149150final DeadlockTest sm;151public boolean done;152}153}154155156