jdk/test/javax/management/standardmbean/DeadlockTest.java
author sjiang
Mon, 15 Dec 2014 19:21:59 +0100
changeset 28061 2a10901eac1b
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
8067241: DeadlockTest.java failed with negative timeout value Reviewed-by: dfuchs, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28061
2a10901eac1b 8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2014, 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
28061
2a10901eac1b 8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents: 5506
diff changeset
    27
 * @summary Test a deadlock and will be blocked forever if the deadlock is present.
2
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
        synchronized(bb) {
28061
2a10901eac1b 8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents: 5506
diff changeset
    59
            while(!bb.gotLock) {
2a10901eac1b 8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents: 5506
diff changeset
    60
                bb.wait(); // if blocked here, means failing to get lock, impossible.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        System.out.println("main: The BadBay is holding the lock forever.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        System.out.println("main: Create a WorkingBoy to see blocking ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        WorkingBoy wb = new WorkingBoy(dt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        synchronized(wb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            wb.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
28061
2a10901eac1b 8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents: 5506
diff changeset
    72
            while(!wb.done) {
2a10901eac1b 8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents: 5506
diff changeset
    73
                wb.wait(); // if blocked here, the deadlock happends
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.out.println("main: OK, bye bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static class BadBoy extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public BadBoy(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            this.o = o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            System.out.println("BadBoy-run: keep synchronization lock forever!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            synchronized(o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    gotLock = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    this.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    Thread.sleep(10000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        final Object o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public boolean gotLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static class WorkingBoy extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        public WorkingBoy(DeadlockTest sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            this.sm = sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                System.out.println("WorkingBoy-run: calling StandardMBean methods ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                System.out.println("WorkingBoy-run: calling setImplementation ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                sm.setImplementation(new Timer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                System.out.println("WorkingBoy-run: calling getImplementation ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                sm.getImplementation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                System.out.println("WorkingBoy-run: calling getMBeanInterface ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                sm.getMBeanInterface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                System.out.println("WorkingBoy-run: calling getImplementationClass ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                sm.getImplementationClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                System.out.println("WorkingBoy-run: calling cacheMBeanInfo ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                sm.cacheMBeanInfo(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                System.out.println("WorkingBoy-run: calling getCachedMBeanInfo ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                sm.getCachedMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                System.out.println("WorkingBoy-run: All done!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } catch (NotCompliantMBeanException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                // Impossible?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw new RuntimeException(ne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        final DeadlockTest sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public boolean done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}