1255N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
1255N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1255N/A *
1255N/A * This code is free software; you can redistribute it and/or modify it
1255N/A * under the terms of the GNU General Public License version 2 only, as
1255N/A * published by the Free Software Foundation.
1255N/A *
1255N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1255N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1255N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1255N/A * version 2 for more details (a copy is included in the LICENSE file that
1255N/A * accompanied this code).
1255N/A *
1255N/A * You should have received a copy of the GNU General Public License version
1255N/A * 2 along with this work; if not, write to the Free Software Foundation,
1255N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1255N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1255N/A */
1255N/A
1255N/A/*
1255N/A * Utility class for ThreadMXBean tests
1255N/A */
1255N/Aimport java.lang.management.ManagementFactory;
1255N/Aimport java.lang.management.ThreadMXBean;
1255N/A
1255N/Apublic class Utils {
1255N/A private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean();
1255N/A private static final int MAX_RETRY = 200;
1255N/A
1255N/A public static boolean waitForBlockWaitingState(Thread t) {
1255N/A // wait for the thread to transition to the expected state
1255N/A int retryCount=0;
1255N/A while (t.getState() == Thread.State.RUNNABLE && retryCount < MAX_RETRY) {
1255N/A goSleep(100);
1255N/A retryCount++;
1255N/A }
1255N/A return (t.getState() != Thread.State.RUNNABLE);
1255N/A }
1255N/A
1255N/A public static boolean waitForThreadState(Thread t, Thread.State expected) {
1255N/A // wait for the thread to transition to the expected state
1255N/A int retryCount=0;
1255N/A while (t.getState() != expected && retryCount < MAX_RETRY) {
1255N/A goSleep(100);
1255N/A retryCount++;
1255N/A }
1255N/A return (t.getState() == expected);
1255N/A }
1255N/A
1255N/A public static void checkThreadState(Thread t, Thread.State expected) {
1255N/A waitForThreadState(t, expected);
1255N/A
1255N/A Thread.State state = tm.getThreadInfo(t.getId()).getThreadState();
1255N/A if (state == null) {
1255N/A throw new RuntimeException(t.getName() + " expected to have " +
1255N/A expected + " but got null.");
1255N/A }
1255N/A if (state != expected) {
1255N/A t.dumpStack();
1255N/A throw new RuntimeException(t.getName() +
1255N/A " expected to have " + expected + " but got " + state);
1255N/A }
1255N/A }
1255N/A
1255N/A public static void goSleep(long ms) {
1255N/A try {
1255N/A Thread.sleep(ms);
1255N/A } catch (InterruptedException e) {
1255N/A e.printStackTrace();
1255N/A System.out.println("TEST FAILED: Unexpected exception.");
1255N/A throw new RuntimeException(e);
1255N/A }
1255N/A }
1255N/A
1255N/A}