Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/notification/BroadcasterSupportDeadlockTest.java
38840 views
/*1* Copyright (c) 2004, 2005, 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* @run clean BroadcasterSupportDeadlockTest30* @run build BroadcasterSupportDeadlockTest31* @run main BroadcasterSupportDeadlockTest32*/3334import java.lang.management.*;35import java.util.concurrent.*;36import javax.management.*;3738public class BroadcasterSupportDeadlockTest {39public static void main(String[] args) throws Exception {40try {41Class.forName(ManagementFactory.class.getName());42} catch (Throwable t) {43System.out.println("TEST CANNOT RUN: needs JDK 5 at least");44return;45}4647final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();48final BroadcasterMBean mbean = new Broadcaster();49final ObjectName name = new ObjectName("test:type=Broadcaster");50mbs.registerMBean(mbean, name);5152ThreadMXBean threads = ManagementFactory.getThreadMXBean();53threads.setThreadContentionMonitoringEnabled(true);5455final Semaphore semaphore = new Semaphore(0);5657// Thread 1 - block the Broadcaster58Thread t1 = new Thread() {59public void run() {60try {61mbs.invoke(name, "block",62new Object[] {semaphore},63new String[] {Semaphore.class.getName()});64} catch (Exception e) {65e.printStackTrace(System.out);66} finally {67System.out.println("TEST INCORRECT: block returned");68System.exit(1);69}70}71};72t1.setDaemon(true);73t1.start();7475/* Wait for Thread 1 to be doing Object.wait(). It's very76difficult to synchronize properly here so we wait for the77semaphore, then wait a little longer for the mbs.invoke to78run, then just in case that isn't enough, we wait for the79thread to be in WAITING state. This isn't foolproof,80because the machine could be very slow and the81Thread.getState() could find the thread in WAITING state82due to some operation it does on its way to the one we're83interested in. */84semaphore.acquire();85Thread.sleep(100);86while (t1.getState() != Thread.State.WAITING)87Thread.sleep(1);8889// Thread 2 - try to add a listener90final NotificationListener listener = new NotificationListener() {91public void handleNotification(Notification n, Object h) {}92};93Thread t2 = new Thread() {94public void run() {95try {96mbs.addNotificationListener(name, listener, null, null);97} catch (Exception e) {98System.out.println("TEST INCORRECT: addNL failed:");99e.printStackTrace(System.out);100}101}102};103t2.setDaemon(true);104t2.start();105106/* Wait for Thread 2 to be blocked on the monitor or to107succeed. */108Thread.sleep(100);109110for (int i = 0; i < 1000/*ms*/; i++) {111t2.join(1/*ms*/);112switch (t2.getState()) {113case TERMINATED:114System.out.println("TEST PASSED");115return;116case BLOCKED:117java.util.Map<Thread,StackTraceElement[]> traces =118Thread.getAllStackTraces();119showStackTrace("Thread 1", traces.get(t1));120showStackTrace("Thread 2", traces.get(t2));121System.out.println("TEST FAILED: deadlock");122System.exit(1);123break;124default:125break;126}127}128129System.out.println("TEST FAILED BUT DID NOT NOTICE DEADLOCK");130Thread.sleep(10000);131System.exit(1);132}133134private static void showStackTrace(String title,135StackTraceElement[] stack) {136System.out.println("---" + title + "---");137if (stack == null)138System.out.println("<no stack trace???>");139else {140for (StackTraceElement elmt : stack)141System.out.println(" " + elmt);142}143System.out.println();144}145146public static interface BroadcasterMBean {147public void block(Semaphore semaphore);148}149150public static class Broadcaster151extends NotificationBroadcasterSupport152implements BroadcasterMBean {153public synchronized void block(Semaphore semaphore) {154Object lock = new Object();155synchronized (lock) {156try {157// Let the caller know that it can now wait for us to158// hit the WAITING state159semaphore.release();160lock.wait(); // block forever161} catch (InterruptedException e) {162System.out.println("TEST INCORRECT: lock interrupted:");163e.printStackTrace(System.out);164System.exit(1);165}166}167}168}169}170171172