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