49 |
|
import java.io.ObjectInputStream; |
50 |
|
import java.io.ObjectOutputStream; |
51 |
|
import java.lang.management.ManagementFactory; |
52 |
+ |
import java.lang.management.LockInfo; |
53 |
|
import java.lang.management.ThreadInfo; |
54 |
|
import java.lang.management.ThreadMXBean; |
55 |
|
import java.lang.reflect.Constructor; |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
+ |
private static final ThreadMXBean THREAD_MXBEAN |
248 |
+ |
= ManagementFactory.getThreadMXBean(); |
249 |
+ |
|
250 |
|
/** |
251 |
|
* The scaling factor to apply to standard delays used in tests. |
252 |
|
* May be initialized from any of: |
358 |
|
// Never report first run of any test; treat it as a |
359 |
|
// warmup run, notably to trigger all needed classloading, |
360 |
|
if (i > 0) |
361 |
< |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
361 |
> |
System.out.printf("%s: %d%n", toString(), elapsedMillis); |
362 |
|
} |
363 |
|
} |
364 |
|
|
704 |
|
/** |
705 |
|
* Returns a random element from given choices. |
706 |
|
*/ |
707 |
+ |
<T> T chooseRandomly(List<T> choices) { |
708 |
+ |
return choices.get(ThreadLocalRandom.current().nextInt(choices.size())); |
709 |
+ |
} |
710 |
+ |
|
711 |
+ |
/** |
712 |
+ |
* Returns a random element from given choices. |
713 |
+ |
*/ |
714 |
|
<T> T chooseRandomly(T... choices) { |
715 |
|
return choices[ThreadLocalRandom.current().nextInt(choices.length)]; |
716 |
|
} |
1094 |
|
} |
1095 |
|
} |
1096 |
|
|
1097 |
+ |
/** Returns true if thread info might be useful in a thread dump. */ |
1098 |
+ |
static boolean threadOfInterest(ThreadInfo info) { |
1099 |
+ |
final String name = info.getThreadName(); |
1100 |
+ |
String lockName; |
1101 |
+ |
if (name == null) |
1102 |
+ |
return true; |
1103 |
+ |
if (name.equals("Signal Dispatcher") |
1104 |
+ |
|| name.equals("WedgedTestDetector")) |
1105 |
+ |
return false; |
1106 |
+ |
if (name.equals("Reference Handler")) { |
1107 |
+ |
// Reference Handler stacktrace changed in JDK-8156500 |
1108 |
+ |
StackTraceElement[] stackTrace; String methodName; |
1109 |
+ |
if ((stackTrace = info.getStackTrace()) != null |
1110 |
+ |
&& stackTrace.length > 0 |
1111 |
+ |
&& (methodName = stackTrace[0].getMethodName()) != null |
1112 |
+ |
&& methodName.equals("waitForReferencePendingList")) |
1113 |
+ |
return false; |
1114 |
+ |
// jdk8 Reference Handler stacktrace |
1115 |
+ |
if ((lockName = info.getLockName()) != null |
1116 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1117 |
+ |
return false; |
1118 |
+ |
} |
1119 |
+ |
if ((name.equals("Finalizer") || name.equals("Common-Cleaner")) |
1120 |
+ |
&& (lockName = info.getLockName()) != null |
1121 |
+ |
&& lockName.startsWith("java.lang.ref")) |
1122 |
+ |
return false; |
1123 |
+ |
if (name.startsWith("ForkJoinPool.commonPool-worker") |
1124 |
+ |
&& (lockName = info.getLockName()) != null |
1125 |
+ |
&& lockName.startsWith("java.util.concurrent.ForkJoinPool")) |
1126 |
+ |
return false; |
1127 |
+ |
return true; |
1128 |
+ |
} |
1129 |
+ |
|
1130 |
|
/** |
1131 |
|
* A debugging tool to print stack traces of most threads, as jstack does. |
1132 |
|
* Uninteresting threads are filtered out. |
1141 |
|
} |
1142 |
|
} |
1143 |
|
|
1100 |
– |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
1144 |
|
System.err.println("------ stacktrace dump start ------"); |
1145 |
< |
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) { |
1146 |
< |
final String name = info.getThreadName(); |
1147 |
< |
String lockName; |
1105 |
< |
if ("Signal Dispatcher".equals(name)) |
1106 |
< |
continue; |
1107 |
< |
if ("Reference Handler".equals(name) |
1108 |
< |
&& (lockName = info.getLockName()) != null |
1109 |
< |
&& lockName.startsWith("java.lang.ref")) |
1110 |
< |
continue; |
1111 |
< |
if (("Finalizer".equals(name) || "Common-Cleaner".equals(name)) |
1112 |
< |
&& (lockName = info.getLockName()) != null |
1113 |
< |
&& lockName.startsWith("java.lang.ref")) |
1114 |
< |
continue; |
1115 |
< |
if ("WedgedTestDetector".equals(name)) |
1116 |
< |
continue; |
1117 |
< |
System.err.print(info); |
1118 |
< |
} |
1145 |
> |
for (ThreadInfo info : THREAD_MXBEAN.dumpAllThreads(true, true)) |
1146 |
> |
if (threadOfInterest(info)) |
1147 |
> |
System.err.print(info); |
1148 |
|
System.err.println("------ stacktrace dump end ------"); |
1149 |
|
|
1150 |
|
if (sm != null) System.setSecurityManager(sm); |
1171 |
|
} |
1172 |
|
|
1173 |
|
/** |
1174 |
+ |
* Returns the thread's blocker's class name, if any, else null. |
1175 |
+ |
*/ |
1176 |
+ |
String blockerClassName(Thread thread) { |
1177 |
+ |
ThreadInfo threadInfo; LockInfo lockInfo; |
1178 |
+ |
if ((threadInfo = THREAD_MXBEAN.getThreadInfo(thread.getId(), 0)) != null |
1179 |
+ |
&& (lockInfo = threadInfo.getLockInfo()) != null) |
1180 |
+ |
return lockInfo.getClassName(); |
1181 |
+ |
return null; |
1182 |
+ |
} |
1183 |
+ |
|
1184 |
+ |
/** |
1185 |
|
* Checks that future.get times out, with the default timeout of |
1186 |
|
* {@code timeoutMillis()}. |
1187 |
|
*/ |
1480 |
|
} |
1481 |
|
|
1482 |
|
/** |
1483 |
+ |
* Returns a new started daemon Thread running the given action, |
1484 |
+ |
* wrapped in a CheckedRunnable. |
1485 |
+ |
*/ |
1486 |
+ |
Thread newStartedThread(Action action) { |
1487 |
+ |
return newStartedThread(checkedRunnable(action)); |
1488 |
+ |
} |
1489 |
+ |
|
1490 |
+ |
/** |
1491 |
|
* Waits for the specified time (in milliseconds) for the thread |
1492 |
|
* to terminate (using {@link Thread#join(long)}), else interrupts |
1493 |
|
* the thread (in the hope that it may terminate later) and fails. |
1534 |
|
} |
1535 |
|
} |
1536 |
|
|
1537 |
+ |
Runnable checkedRunnable(Action action) { |
1538 |
+ |
return new CheckedRunnable() { |
1539 |
+ |
public void realRun() throws Throwable { |
1540 |
+ |
action.run(); |
1541 |
+ |
}}; |
1542 |
+ |
} |
1543 |
+ |
|
1544 |
|
public abstract class ThreadShouldThrow extends Thread { |
1545 |
|
protected abstract void realRun() throws Throwable; |
1546 |
|
|
1779 |
|
|
1780 |
|
public int await() { |
1781 |
|
try { |
1782 |
< |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1782 |
> |
return super.await(LONGER_DELAY_MS, MILLISECONDS); |
1783 |
|
} catch (TimeoutException timedOut) { |
1784 |
|
throw new AssertionError("timed out"); |
1785 |
|
} catch (Exception fail) { |