jdk/test/javax/management/standardmbean/DeadlockTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 28061 2a10901eac1b
child 44423 306c020eb154
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 28061
diff changeset
     2
 * Copyright (c) 2005, 2015, 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
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 28061
diff changeset
    29
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run main DeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.timer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class DeadlockTest extends StandardMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public <T> DeadlockTest(T implementation, Class<T> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        super(implementation, mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public MBeanInfo getCachedMBeanInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        return super.getCachedMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public void cacheMBeanInfo(MBeanInfo mi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        super.cacheMBeanInfo(mi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.out.println("main: No deadlock please.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        System.out.println("main: Create a BadBay to hold the lock forever.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        DeadlockTest dt = new DeadlockTest(new Timer(), TimerMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        BadBoy bb = new BadBoy(dt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        bb.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        synchronized(bb) {
28061
2a10901eac1b 8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents: 5506
diff changeset
    60
            while(!bb.gotLock) {
2a10901eac1b 8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents: 5506
diff changeset
    61
                bb.wait(); // if blocked here, means failing to get lock, impossible.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        System.out.println("main: The BadBay is holding the lock forever.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        System.out.println("main: Create a WorkingBoy to see blocking ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        WorkingBoy wb = new WorkingBoy(dt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        synchronized(wb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            wb.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
28061
2a10901eac1b 8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents: 5506
diff changeset
    73
            while(!wb.done) {
2a10901eac1b 8067241: DeadlockTest.java failed with negative timeout value
sjiang
parents: 5506
diff changeset
    74
                wb.wait(); // if blocked here, the deadlock happends
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.out.println("main: OK, bye bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static class BadBoy extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public BadBoy(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            this.o = o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            System.out.println("BadBoy-run: keep synchronization lock forever!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            synchronized(o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    gotLock = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    this.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    Thread.sleep(10000000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    // OK
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        final Object o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        public boolean gotLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static class WorkingBoy extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        public WorkingBoy(DeadlockTest sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            this.sm = sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                System.out.println("WorkingBoy-run: calling StandardMBean methods ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                System.out.println("WorkingBoy-run: calling setImplementation ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                sm.setImplementation(new Timer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                System.out.println("WorkingBoy-run: calling getImplementation ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                sm.getImplementation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                System.out.println("WorkingBoy-run: calling getMBeanInterface ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                sm.getMBeanInterface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                System.out.println("WorkingBoy-run: calling getImplementationClass ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                sm.getImplementationClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                System.out.println("WorkingBoy-run: calling cacheMBeanInfo ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                sm.cacheMBeanInfo(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                System.out.println("WorkingBoy-run: calling getCachedMBeanInfo ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                sm.getCachedMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                System.out.println("WorkingBoy-run: All done!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } catch (NotCompliantMBeanException ne) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // Impossible?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                throw new RuntimeException(ne);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        final DeadlockTest sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        public boolean done;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}