Path: blob/master/test/jdk/javax/management/standardmbean/DeadlockTest.java
41149 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*/2223/*24* @test25* @bug 633174626* @summary Test a deadlock and will be blocked forever if the deadlock is present.27* @author Shanliang JIANG28*29* @run main DeadlockTest30*/3132import javax.management.*;33import javax.management.timer.*;3435public class DeadlockTest extends StandardMBean {36public <T> DeadlockTest(T implementation, Class<T> mbeanInterface)37throws NotCompliantMBeanException {38super(implementation, mbeanInterface);39}4041public MBeanInfo getCachedMBeanInfo() {42return super.getCachedMBeanInfo();43}4445public void cacheMBeanInfo(MBeanInfo mi) {46super.cacheMBeanInfo(mi);47}4849public static void main(String[] args) throws Exception {50System.out.println("main: No deadlock please.");5152System.out.println("main: Create a BadBay to hold the lock forever.");53DeadlockTest dt = new DeadlockTest(new Timer(), TimerMBean.class);5455BadBoy bb = new BadBoy(dt);56bb.start();5758synchronized(bb) {59while(!bb.gotLock) {60bb.wait(); // if blocked here, means failing to get lock, impossible.61}62}6364System.out.println("main: The BadBay is holding the lock forever.");6566System.out.println("main: Create a WorkingBoy to see blocking ...");67WorkingBoy wb = new WorkingBoy(dt);6869synchronized(wb) {70wb.start();7172while(!wb.done) {73wb.wait(); // if blocked here, the deadlock happends74}75}7677System.out.println("main: OK, bye bye.");78}7980private static class BadBoy extends Thread {81public BadBoy(Object o) {82setDaemon(true);8384this.o = o;85}8687public void run() {88System.out.println("BadBoy-run: keep synchronization lock forever!");8990synchronized(o) {91synchronized(this) {92gotLock = true;9394this.notify();95}9697try {98Thread.sleep(10000000);99} catch (Exception e) {100// OK101}102}103}104105final Object o;106public boolean gotLock;107}108109private static class WorkingBoy extends Thread {110public WorkingBoy(DeadlockTest sm) {111setDaemon(true);112113this.sm = sm;114}115116public void run() {117try {118System.out.println("WorkingBoy-run: calling StandardMBean methods ...");119120System.out.println("WorkingBoy-run: calling setImplementation ...");121sm.setImplementation(new Timer());122123System.out.println("WorkingBoy-run: calling getImplementation ...");124sm.getImplementation();125126System.out.println("WorkingBoy-run: calling getMBeanInterface ...");127sm.getMBeanInterface();128129System.out.println("WorkingBoy-run: calling getImplementationClass ...");130sm.getImplementationClass();131132System.out.println("WorkingBoy-run: calling cacheMBeanInfo ...");133sm.cacheMBeanInfo(null);134135System.out.println("WorkingBoy-run: calling getCachedMBeanInfo ...");136sm.getCachedMBeanInfo();137138System.out.println("WorkingBoy-run: All done!");139140synchronized(this) {141done = true;142143this.notifyAll();144}145} catch (NotCompliantMBeanException ne) {146// Impossible?147throw new RuntimeException(ne);148}149}150151final DeadlockTest sm;152public boolean done;153}154}155156157