1 /*
   2  * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6331746
  27  * @summary Test a deadlock and will be blocked forever if the deadlock is present.
  28  * @author Shanliang JIANG
  29  * @modules java.management
  30  * @run main DeadlockTest
  31  */
  32 
  33 import javax.management.*;
  34 import javax.management.timer.*;
  35 
  36 public class DeadlockTest extends StandardMBean {
  37     public <T> DeadlockTest(T implementation, Class<T> mbeanInterface)
  38     throws NotCompliantMBeanException {
  39         super(implementation, mbeanInterface);
  40     }
  41 
  42     public MBeanInfo getCachedMBeanInfo() {
  43         return super.getCachedMBeanInfo();
  44     }
  45 
  46     public void cacheMBeanInfo(MBeanInfo mi) {
  47         super.cacheMBeanInfo(mi);
  48     }
  49 
  50     public static void main(String[] args) throws Exception {
  51         System.out.println("main: No deadlock please.");
  52 
  53         System.out.println("main: Create a BadBay to hold the lock forever.");
  54         DeadlockTest dt = new DeadlockTest(new Timer(), TimerMBean.class);
  55 
  56         BadBoy bb = new BadBoy(dt);
  57         bb.start();
  58 
  59         synchronized(bb) {
  60             while(!bb.gotLock) {
  61                 bb.wait(); // if blocked here, means failing to get lock, impossible.
  62             }
  63         }
  64 
  65         System.out.println("main: The BadBay is holding the lock forever.");
  66 
  67         System.out.println("main: Create a WorkingBoy to see blocking ...");
  68         WorkingBoy wb = new WorkingBoy(dt);
  69 
  70         synchronized(wb) {
  71             wb.start();
  72 
  73             while(!wb.done) {
  74                 wb.wait(); // if blocked here, the deadlock happends
  75             }
  76         }
  77 
  78         System.out.println("main: OK, bye bye.");
  79     }
  80 
  81     private static class BadBoy extends Thread {
  82         public BadBoy(Object o) {
  83             setDaemon(true);
  84 
  85             this.o = o;
  86         }
  87 
  88         public void run() {
  89             System.out.println("BadBoy-run: keep synchronization lock forever!");
  90 
  91             synchronized(o) {
  92                 synchronized(this) {
  93                     gotLock = true;
  94 
  95                     this.notify();
  96                 }
  97 
  98                 try {
  99                     Thread.sleep(10000000);
 100                 } catch (Exception e) {
 101                     // OK
 102                 }
 103             }
 104         }
 105 
 106         final Object o;
 107         public boolean gotLock;
 108     }
 109 
 110     private static class WorkingBoy extends Thread {
 111         public WorkingBoy(DeadlockTest sm) {
 112             setDaemon(true);
 113 
 114             this.sm = sm;
 115         }
 116 
 117         public void run() {
 118             try {
 119                 System.out.println("WorkingBoy-run: calling StandardMBean methods ...");
 120 
 121                 System.out.println("WorkingBoy-run: calling setImplementation ...");
 122                 sm.setImplementation(new Timer());
 123 
 124                 System.out.println("WorkingBoy-run: calling getImplementation ...");
 125                 sm.getImplementation();
 126 
 127                 System.out.println("WorkingBoy-run: calling getMBeanInterface ...");
 128                 sm.getMBeanInterface();
 129 
 130                 System.out.println("WorkingBoy-run: calling getImplementationClass ...");
 131                 sm.getImplementationClass();
 132 
 133                 System.out.println("WorkingBoy-run: calling cacheMBeanInfo ...");
 134                 sm.cacheMBeanInfo(null);
 135 
 136                 System.out.println("WorkingBoy-run: calling getCachedMBeanInfo ...");
 137                 sm.getCachedMBeanInfo();
 138 
 139                 System.out.println("WorkingBoy-run: All done!");
 140 
 141                 synchronized(this) {
 142                     done = true;
 143 
 144                     this.notifyAll();
 145                 }
 146             } catch (NotCompliantMBeanException ne) {
 147                 // Impossible?
 148                 throw new RuntimeException(ne);
 149             }
 150         }
 151 
 152         final DeadlockTest sm;
 153         public boolean done;
 154     }
 155 }