Path: blob/master/test/jdk/javax/management/notification/BroadcasterSupportDeadlockTest.java
41149 views
/*1* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 5093922 212005526* @summary Test that NotificationBroadcasterSupport can be subclassed27* and used with synchronized(this) without causing deadlock28* @author Eamonn McManus29*30* @run clean BroadcasterSupportDeadlockTest31* @run build BroadcasterSupportDeadlockTest32* @run main BroadcasterSupportDeadlockTest33*/3435import java.lang.management.*;36import java.util.concurrent.*;37import javax.management.*;3839public class BroadcasterSupportDeadlockTest {40public static void main(String[] args) throws Exception {41try {42Class.forName(ManagementFactory.class.getName());43} catch (Throwable t) {44System.out.println("TEST CANNOT RUN: needs JDK 5 at least");45return;46}4748final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();49final BroadcasterMBean mbean = new Broadcaster();50final ObjectName name = new ObjectName("test:type=Broadcaster");51mbs.registerMBean(mbean, name);5253ThreadMXBean threads = ManagementFactory.getThreadMXBean();54threads.setThreadContentionMonitoringEnabled(true);5556final Semaphore semaphore = new Semaphore(0);5758// Thread 1 - block the Broadcaster59Thread t1 = new Thread() {60public void run() {61try {62mbs.invoke(name, "block",63new Object[] {semaphore},64new String[] {Semaphore.class.getName()});65} catch (Exception e) {66e.printStackTrace(System.out);67} finally {68System.out.println("TEST INCORRECT: block returned");69System.exit(1);70}71}72};73t1.setDaemon(true);74t1.start();7576/* Wait for Thread 1 to be doing Object.wait(). It's very77difficult to synchronize properly here so we wait for the78semaphore, then wait a little longer for the mbs.invoke to79run, then just in case that isn't enough, we wait for the80thread to be in WAITING state. This isn't foolproof,81because the machine could be very slow and the82Thread.getState() could find the thread in WAITING state83due to some operation it does on its way to the one we're84interested in. */85semaphore.acquire();86Thread.sleep(100);87while (t1.getState() != Thread.State.WAITING)88Thread.sleep(1);8990// Thread 2 - try to add a listener91final NotificationListener listener = new NotificationListener() {92public void handleNotification(Notification n, Object h) {}93};94Thread t2 = new Thread() {95public void run() {96try {97mbs.addNotificationListener(name, listener, null, null);98} catch (Exception e) {99System.out.println("TEST INCORRECT: addNL failed:");100e.printStackTrace(System.out);101}102}103};104t2.setDaemon(true);105t2.start();106107/* Wait for Thread 2 to be blocked on the monitor or to108succeed. */109Thread.sleep(100);110111for (int i = 0; i < 1000/*ms*/; i++) {112t2.join(1/*ms*/);113switch (t2.getState()) {114case TERMINATED:115System.out.println("TEST PASSED");116return;117case BLOCKED:118java.util.Map<Thread,StackTraceElement[]> traces =119Thread.getAllStackTraces();120showStackTrace("Thread 1", traces.get(t1));121showStackTrace("Thread 2", traces.get(t2));122System.out.println("TEST FAILED: deadlock");123System.exit(1);124break;125default:126break;127}128}129130System.out.println("TEST FAILED BUT DID NOT NOTICE DEADLOCK");131Thread.sleep(10000);132System.exit(1);133}134135private static void showStackTrace(String title,136StackTraceElement[] stack) {137System.out.println("---" + title + "---");138if (stack == null)139System.out.println("<no stack trace???>");140else {141for (StackTraceElement elmt : stack)142System.out.println(" " + elmt);143}144System.out.println();145}146147public static interface BroadcasterMBean {148public void block(Semaphore semaphore);149}150151public static class Broadcaster152extends NotificationBroadcasterSupport153implements BroadcasterMBean {154public synchronized void block(Semaphore semaphore) {155Object lock = new Object();156synchronized (lock) {157try {158// Let the caller know that it can now wait for us to159// hit the WAITING state160semaphore.release();161lock.wait(); // block forever162} catch (InterruptedException e) {163System.out.println("TEST INCORRECT: lock interrupted:");164e.printStackTrace(System.out);165System.exit(1);166}167}168}169}170}171172173