提交 2787ea2f 编写于 作者: J jbachorik

6712222: Race condition in java/lang/management/ThreadMXBean/AllThreadIds.java

Reviewed-by: dholmes, dfuchs
上级 472c3367
/* /*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -27,21 +27,19 @@ ...@@ -27,21 +27,19 @@
* @summary Basic unit test of ThreadMXBean.getAllThreadIds() * @summary Basic unit test of ThreadMXBean.getAllThreadIds()
* @author Alexei Guibadoulline and Mandy Chung * @author Alexei Guibadoulline and Mandy Chung
* *
* @run build Barrier
* @run main/othervm AllThreadIds * @run main/othervm AllThreadIds
*/ */
import java.lang.management.*; import java.lang.management.*;
import java.util.*; import java.util.concurrent.Phaser;
public class AllThreadIds { public class AllThreadIds {
final static int DAEMON_THREADS = 20; final static int DAEMON_THREADS = 20;
final static int USER_THREADS = 5; final static int USER_THREADS = 5;
final static int ALL_THREADS = DAEMON_THREADS + USER_THREADS; final static int ALL_THREADS = DAEMON_THREADS + USER_THREADS;
private static volatile boolean live[] = new boolean[ALL_THREADS]; private static final boolean live[] = new boolean[ALL_THREADS];
private static Thread allThreads[] = new Thread[ALL_THREADS]; private static final Thread allThreads[] = new Thread[ALL_THREADS];
private static ThreadMXBean mbean private static final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
= ManagementFactory.getThreadMXBean();
private static boolean testFailed = false; private static boolean testFailed = false;
private static boolean trace = false; private static boolean trace = false;
...@@ -52,8 +50,7 @@ public class AllThreadIds { ...@@ -52,8 +50,7 @@ public class AllThreadIds {
private static int curLiveThreadCount = 0; private static int curLiveThreadCount = 0;
private static int curPeakThreadCount = 0; private static int curPeakThreadCount = 0;
// barrier for threads communication private static final Phaser startupCheck = new Phaser(ALL_THREADS + 1);
private static Barrier barrier = new Barrier(ALL_THREADS);
private static void printThreadList() { private static void printThreadList() {
if (!trace) return; if (!trace) return;
...@@ -124,18 +121,15 @@ public class AllThreadIds { ...@@ -124,18 +121,15 @@ public class AllThreadIds {
curPeakThreadCount = mbean.getPeakThreadCount(); curPeakThreadCount = mbean.getPeakThreadCount();
checkThreadCount(0, 0); checkThreadCount(0, 0);
// Start all threads and wait to be sure they all are alive // Start all threads and wait to be sure they all are alive
barrier.set(ALL_THREADS);
for (int i = 0; i < ALL_THREADS; i++) { for (int i = 0; i < ALL_THREADS; i++) {
live[i] = true; setLive(i, true);
allThreads[i] = new MyThread(i); allThreads[i] = new MyThread(i);
allThreads[i].setDaemon( (i < DAEMON_THREADS) ? true : false); allThreads[i].setDaemon(i < DAEMON_THREADS);
allThreads[i].start(); allThreads[i].start();
} }
// wait until all threads are started. // wait until all threads are started.
barrier.await(); startupCheck.arriveAndAwaitAdvance();
checkThreadCount(ALL_THREADS, 0); checkThreadCount(ALL_THREADS, 0);
printThreadList(); printThreadList();
...@@ -173,15 +167,14 @@ public class AllThreadIds { ...@@ -173,15 +167,14 @@ public class AllThreadIds {
// Stop daemon threads, wait to be sure they all are dead, and check // Stop daemon threads, wait to be sure they all are dead, and check
// that they disappeared from getAllThreadIds() list // that they disappeared from getAllThreadIds() list
barrier.set(DAEMON_THREADS);
for (int i = 0; i < DAEMON_THREADS; i++) { for (int i = 0; i < DAEMON_THREADS; i++) {
live[i] = false; setLive(i, false);
} }
// wait until daemon threads are terminated.
barrier.await();
// give chance to threads to terminate // make sure the daemon threads are completely dead
pause(); joinDaemonThreads();
// and check the reported thread count
checkThreadCount(0, DAEMON_THREADS); checkThreadCount(0, DAEMON_THREADS);
// Check mbean now // Check mbean now
...@@ -190,11 +183,11 @@ public class AllThreadIds { ...@@ -190,11 +183,11 @@ public class AllThreadIds {
for (int i = 0; i < ALL_THREADS; i++) { for (int i = 0; i < ALL_THREADS; i++) {
long expectedId = allThreads[i].getId(); long expectedId = allThreads[i].getId();
boolean found = false; boolean found = false;
boolean live = (i >= DAEMON_THREADS); boolean alive = (i >= DAEMON_THREADS);
if (trace) { if (trace) {
System.out.print("Looking for thread with id " + expectedId + System.out.print("Looking for thread with id " + expectedId +
(live ? " expected alive." : " expected terminated.")); (alive ? " expected alive." : " expected terminated."));
} }
for (int j = 0; j < list.length; j++) { for (int j = 0; j < list.length; j++) {
if (expectedId == list[j]) { if (expectedId == list[j]) {
...@@ -203,11 +196,11 @@ public class AllThreadIds { ...@@ -203,11 +196,11 @@ public class AllThreadIds {
} }
} }
if (live != found) { if (alive != found) {
testFailed = true; testFailed = true;
} }
if (trace) { if (trace) {
if (live != found) { if (alive != found) {
System.out.println(" TEST FAILED."); System.out.println(" TEST FAILED.");
} else { } else {
System.out.println(); System.out.println();
...@@ -216,15 +209,14 @@ public class AllThreadIds { ...@@ -216,15 +209,14 @@ public class AllThreadIds {
} }
// Stop all threads and wait to be sure they all are dead // Stop all threads and wait to be sure they all are dead
barrier.set(ALL_THREADS - DAEMON_THREADS);
for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) { for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) {
live[i] = false; setLive(i, false);
} }
// wait until daemon threads are terminated .
barrier.await();
// give chance to threads to terminate // make sure the non-daemon threads are completely dead
pause(); joinNonDaemonThreads();
// and check the thread count
checkThreadCount(0, ALL_THREADS - DAEMON_THREADS); checkThreadCount(0, ALL_THREADS - DAEMON_THREADS);
if (testFailed) if (testFailed)
...@@ -233,6 +225,30 @@ public class AllThreadIds { ...@@ -233,6 +225,30 @@ public class AllThreadIds {
System.out.println("Test passed."); System.out.println("Test passed.");
} }
private static void joinDaemonThreads() throws InterruptedException {
for (int i = 0; i < DAEMON_THREADS; i++) {
allThreads[i].join();
}
}
private static void joinNonDaemonThreads() throws InterruptedException {
for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) {
allThreads[i].join();
}
}
private static void setLive(int i, boolean val) {
synchronized(live) {
live[i] = val;
}
}
private static boolean isLive(int i) {
synchronized(live) {
return live[i];
}
}
// The MyThread thread lives as long as correspondent live[i] value is true // The MyThread thread lives as long as correspondent live[i] value is true
private static class MyThread extends Thread { private static class MyThread extends Thread {
int id; int id;
...@@ -243,8 +259,8 @@ public class AllThreadIds { ...@@ -243,8 +259,8 @@ public class AllThreadIds {
public void run() { public void run() {
// signal started // signal started
barrier.signal(); startupCheck.arrive();
while (live[id]) { while (isLive(id)) {
try { try {
sleep(100); sleep(100);
} catch (InterruptedException e) { } catch (InterruptedException e) {
...@@ -253,23 +269,6 @@ public class AllThreadIds { ...@@ -253,23 +269,6 @@ public class AllThreadIds {
testFailed = true; testFailed = true;
} }
} }
// signal about to exit
barrier.signal();
} }
} }
private static Object pauseObj = new Object();
private static void pause() {
// Enter lock a without blocking
synchronized (pauseObj) {
try {
// may need to tune this timeout for different platforms
pauseObj.wait(50);
} catch (Exception e) {
System.err.println("Unexpected exception.");
e.printStackTrace(System.err);
}
}
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册