jdk/test/javax/management/notification/BroadcasterSupportDeadlockTest.java
author darcy
Fri, 19 Aug 2011 17:42:24 -0700
changeset 10350 6d009f117062
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
4748706: typos in java.util.Observable documentation Reviewed-by: iris
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) 2004, 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 5093922 2120055
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that NotificationBroadcasterSupport can be subclassed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * and used with synchronized(this) without causing deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Eamonn McManus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean BroadcasterSupportDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build BroadcasterSupportDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main BroadcasterSupportDeadlockTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class BroadcasterSupportDeadlockTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            Class.forName(ManagementFactory.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            System.out.println("TEST CANNOT RUN: needs JDK 5 at least");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        final BroadcasterMBean mbean = new Broadcaster();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        final ObjectName name = new ObjectName("test:type=Broadcaster");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        mbs.registerMBean(mbean, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        ThreadMXBean threads = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        threads.setThreadContentionMonitoringEnabled(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        final Semaphore semaphore = new Semaphore(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // Thread 1 - block the Broadcaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Thread t1 = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    mbs.invoke(name, "block",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                               new Object[] {semaphore},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                               new String[] {Semaphore.class.getName()});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    System.out.println("TEST INCORRECT: block returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        t1.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        t1.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        /* Wait for Thread 1 to be doing Object.wait().  It's very
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
           difficult to synchronize properly here so we wait for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
           semaphore, then wait a little longer for the mbs.invoke to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
           run, then just in case that isn't enough, we wait for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
           thread to be in WAITING state.  This isn't foolproof,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
           because the machine could be very slow and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
           Thread.getState() could find the thread in WAITING state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
           due to some operation it does on its way to the one we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
           interested in.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        semaphore.acquire();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        while (t1.getState() != Thread.State.WAITING)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            Thread.sleep(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // Thread 2 - try to add a listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        final NotificationListener listener = new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            public void handleNotification(Notification n, Object h) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        Thread t2 = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    mbs.addNotificationListener(name, listener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    System.out.println("TEST INCORRECT: addNL failed:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    e.printStackTrace(System.out);
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
        t2.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        t2.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        /* Wait for Thread 2 to be blocked on the monitor or to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
           succeed.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        for (int i = 0; i < 1000/*ms*/; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            t2.join(1/*ms*/);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            switch (t2.getState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            case TERMINATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                System.out.println("TEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            case BLOCKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                java.util.Map<Thread,StackTraceElement[]> traces =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    Thread.getAllStackTraces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                showStackTrace("Thread 1", traces.get(t1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                showStackTrace("Thread 2", traces.get(t2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                System.out.println("TEST FAILED: deadlock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.out.println("TEST FAILED BUT DID NOT NOTICE DEADLOCK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Thread.sleep(10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private static void showStackTrace(String title,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                       StackTraceElement[] stack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        System.out.println("---" + title + "---");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (stack == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.out.println("<no stack trace???>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            for (StackTraceElement elmt : stack)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.out.println("    " + elmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public static interface BroadcasterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        public void block(Semaphore semaphore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static class Broadcaster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            implements BroadcasterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public synchronized void block(Semaphore semaphore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    // Let the caller know that it can now wait for us to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    // hit the WAITING state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    semaphore.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    lock.wait(); // block forever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    System.out.println("TEST INCORRECT: lock interrupted:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    System.exit(1);
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}