Skip to content

Commit 58911cc

Browse files
committed
8188784: javax/management/notification/BroadcasterSupportDeadlockTest.java - TEST FAILED: deadlock
Reviewed-by: cjplummer, lmesnik
1 parent 97c1808 commit 58911cc

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

test/jdk/javax/management/notification/BroadcasterSupportDeadlockTest.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -38,13 +38,8 @@
3838
import javax.management.*;
3939

4040
public class BroadcasterSupportDeadlockTest {
41+
4142
public static void main(String[] args) throws Exception {
42-
try {
43-
Class.forName(ManagementFactory.class.getName());
44-
} catch (Throwable t) {
45-
System.out.println("TEST CANNOT RUN: needs JDK 5 at least");
46-
return;
47-
}
4843

4944
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
5045
final BroadcasterMBean mbean = new Broadcaster();
@@ -66,8 +61,7 @@ public void run() {
6661
} catch (Exception e) {
6762
e.printStackTrace(System.out);
6863
} finally {
69-
System.out.println("TEST INCORRECT: block returned");
70-
System.exit(1);
64+
throw new RuntimeException("TEST INCORRECT: block returned");
7165
}
7266
}
7367
};
@@ -85,8 +79,9 @@ public void run() {
8579
interested in. */
8680
semaphore.acquire();
8781
Thread.sleep(100);
88-
while (t1.getState() != Thread.State.WAITING)
89-
Thread.sleep(1);
82+
while (t1.getState() != Thread.State.WAITING) {
83+
Thread.sleep(100);
84+
}
9085

9186
// Thread 2 - try to add a listener
9287
final NotificationListener listener = new NotificationListener() {
@@ -105,32 +100,26 @@ public void run() {
105100
t2.setDaemon(true);
106101
t2.start();
107102

108-
/* Wait for Thread 2 to be blocked on the monitor or to
109-
succeed. */
103+
/* Wait for Thread 2 to be blocked on the monitor or to succeed. */
110104
Thread.sleep(100);
111105

112-
for (int i = 0; i < 1000/*ms*/; i++) {
113-
t2.join(1/*ms*/);
114-
switch (t2.getState()) {
115-
case TERMINATED:
106+
for (int i = 0; i < 1000; i++) {
107+
t2.join(100 /*ms*/);
108+
if (t2.getState() == Thread.State.TERMINATED) {
116109
System.out.println("TEST PASSED");
117110
return;
118-
case BLOCKED:
119-
java.util.Map<Thread,StackTraceElement[]> traces =
120-
Thread.getAllStackTraces();
121-
showStackTrace("Thread 1", traces.get(t1));
122-
showStackTrace("Thread 2", traces.get(t2));
123-
System.out.println("TEST FAILED: deadlock");
124-
System.exit(1);
125-
break;
126-
default:
127-
break;
128111
}
129112
}
130113

131-
System.out.println("TEST FAILED BUT DID NOT NOTICE DEADLOCK");
132-
Thread.sleep(10000);
133-
System.exit(1);
114+
if (t2.getState() == Thread.State.BLOCKED) {
115+
System.out.println("TEST FAILED: deadlock");
116+
} else {
117+
System.out.println("TEST FAILED BUT DID NOT NOTICE DEADLOCK (state = " + t2.getState() + ")");
118+
}
119+
java.util.Map<Thread,StackTraceElement[]> traces = Thread.getAllStackTraces();
120+
showStackTrace("Thread 1", traces.get(t1));
121+
showStackTrace("Thread 2", traces.get(t2));
122+
throw new RuntimeException("TEST FAILED");
134123
}
135124

136125
private static void showStackTrace(String title,
@@ -152,6 +141,7 @@ public static interface BroadcasterMBean {
152141
public static class Broadcaster
153142
extends NotificationBroadcasterSupport
154143
implements BroadcasterMBean {
144+
155145
public synchronized void block(Semaphore semaphore) {
156146
Object lock = new Object();
157147
synchronized (lock) {
@@ -161,9 +151,7 @@ public synchronized void block(Semaphore semaphore) {
161151
semaphore.release();
162152
lock.wait(); // block forever
163153
} catch (InterruptedException e) {
164-
System.out.println("TEST INCORRECT: lock interrupted:");
165-
e.printStackTrace(System.out);
166-
System.exit(1);
154+
throw new RuntimeException("TEST INCORRECT: lock interrupted");
167155
}
168156
}
169157
}

0 commit comments

Comments
 (0)