/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4530538
* @summary Basic unit test of ThreadMXBean.getAllThreadIds()
* @author Alexei Guibadoulline and Mandy Chung
*
* @run build Barrier
*/
public class AllThreadIds {
private static boolean testFailed = false;
private static boolean trace = false;
// barrier for threads communication
private static void printThreadList() {
if (!trace) return;
}
for (int i = 0; i < ALL_THREADS; i++) {
Thread t = allThreads[i];
" die = " + live[i] +
" alive = " + t.isAlive());
}
}
trace = true;
throw new RuntimeException(msg);
}
int numTerminatedThreads)
throws Exception {
if ((curLiveThreadCount - prevLiveThreadCount) !=
fail("Unexpected number of live threads: " +
" Prev live = " + prevLiveThreadCount +
" Current live = " + curLiveThreadCount +
" Threads added = " + numNewThreads +
" Threads terminated = " + numTerminatedThreads);
}
fail("Unexpected number of peak threads: " +
" Prev peak = " + prevPeakThreadCount +
" Current peak = " + curPeakThreadCount +
" Threads added = " + numNewThreads);
}
fail("Unexpected number of total threads: " +
" Prev Total = " + prevTotalThreadCount +
" Current Total = " + curTotalThreadCount +
" Threads added = " + numNewThreads);
}
fail("Array length returned by " +
" not matched count = " + curLiveThreadCount);
}
}
trace = true;
}
// Start all threads and wait to be sure they all are alive
for (int i = 0; i < ALL_THREADS; i++) {
live[i] = true;
allThreads[i] = new MyThread(i);
allThreads[i].start();
}
// wait until all threads are started.
// Check mbean now. All threads must appear in getAllThreadIds() list
for (int i = 0; i < ALL_THREADS; i++) {
boolean found = false;
if (trace) {
}
if (expectedId == list[j]) {
found = true;
break;
}
}
if (!found) {
testFailed = true;
}
if (trace) {
if (!found) {
}
}
}
if (trace) {
}
// Stop daemon threads, wait to be sure they all are dead, and check
// that they disappeared from getAllThreadIds() list
for (int i = 0; i < DAEMON_THREADS; i++) {
live[i] = false;
}
// wait until daemon threads are terminated.
// give chance to threads to terminate
pause();
// Check mbean now
for (int i = 0; i < ALL_THREADS; i++) {
boolean found = false;
boolean live = (i >= DAEMON_THREADS);
if (trace) {
}
if (expectedId == list[j]) {
found = true;
break;
}
}
testFailed = true;
}
if (trace) {
} else {
}
}
}
// Stop all threads and wait to be sure they all are dead
for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) {
live[i] = false;
}
// wait until daemon threads are terminated .
// give chance to threads to terminate
pause();
if (testFailed)
throw new RuntimeException("TEST FAILED.");
}
// The MyThread thread lives as long as correspondent live[i] value is true
int id;
}
public void run() {
// signal started
try {
sleep(100);
} catch (InterruptedException e) {
testFailed = true;
}
}
// signal about to exit
}
}
private static void pause() {
// Enter lock a without blocking
synchronized (pauseObj) {
try {
// may need to tune this timeout for different platforms
} catch (Exception e) {
}
}
}
}