Path: blob/master/test/jdk/java/lang/management/ThreadMXBean/Utils.java
41152 views
/*1* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Utility class for ThreadMXBean tests25*/26import java.lang.management.ManagementFactory;27import java.lang.management.ThreadMXBean;2829public class Utils {30private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean();31private static final int MAX_RETRY = 200;3233public static boolean waitForBlockWaitingState(Thread t) {34// wait for the thread to transition to the expected state35int retryCount=0;36while (t.getState() == Thread.State.RUNNABLE && retryCount < MAX_RETRY) {37goSleep(100);38retryCount++;39}40return (t.getState() != Thread.State.RUNNABLE);41}4243public static boolean waitForThreadState(Thread t, Thread.State expected) {44// wait for the thread to transition to the expected state45int retryCount=0;46while (t.getState() != expected && retryCount < MAX_RETRY) {47goSleep(100);48retryCount++;49}50return (t.getState() == expected);51}5253public static void checkThreadState(Thread t, Thread.State expected) {54waitForThreadState(t, expected);5556Thread.State state = tm.getThreadInfo(t.getId()).getThreadState();57if (state == null) {58throw new RuntimeException(t.getName() + " expected to have " +59expected + " but got null.");60}61if (state != expected) {62t.dumpStack();63throw new RuntimeException(t.getName() +64" expected to have " + expected + " but got " + state);65}66}6768public static void goSleep(long ms) {69try {70Thread.sleep(ms);71} catch (InterruptedException e) {72e.printStackTrace();73System.out.println("TEST FAILED: Unexpected exception.");74throw new RuntimeException(e);75}76}7778}798081