Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/ThreadMXBean/ThreadMXBeanStateTest.java
38821 views
/*1* Copyright (c) 2003, 2014, 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* @test25* @bug 4967283 5080203 802220826* @summary Basic unit test of thread states returned by27* ThreadMXBean.getThreadInfo.getThreadState().28* It also tests lock information returned by ThreadInfo.29*30* @author Mandy Chung31*32* @library ../../Thread33* @library /lib/testlibrary34* @build jdk.testlibrary.*35* @build ThreadMXBeanStateTest ThreadStateController36* @run main ThreadMXBeanStateTest37*/3839import java.lang.management.ManagementFactory;40import java.lang.management.ThreadMXBean;41import java.lang.management.ThreadInfo;42import static java.lang.Thread.State.*;4344public class ThreadMXBeanStateTest {45private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean();4647static class Lock {48private final String name;49Lock(String name) {50this.name = name;51}52@Override53public String toString() {54return name;55}56}5758private static final Lock globalLock = new Lock("my lock");5960public static void main(String[] argv) throws Exception {61// Force thread state initialization now before the test62// verification begins.63Thread.currentThread().getState();64ThreadStateController thread = new ThreadStateController("StateChanger", globalLock);65thread.setDaemon(true);6667// before myThread starts68thread.checkThreadState(NEW);6970thread.start();71thread.transitionTo(RUNNABLE);72thread.checkThreadState(RUNNABLE);73checkLockInfo(thread, RUNNABLE, null, null);7475thread.suspend();76ThreadStateController.pause(10);77thread.checkThreadState(RUNNABLE);78checkSuspendedThreadState(thread, RUNNABLE);79thread.resume();8081synchronized (globalLock) {82thread.transitionTo(BLOCKED);83thread.checkThreadState(BLOCKED);84checkLockInfo(thread, BLOCKED,85globalLock, Thread.currentThread());86}8788thread.transitionTo(WAITING);89thread.checkThreadState(WAITING);90checkLockInfo(thread, Thread.State.WAITING,91globalLock, null);9293thread.transitionTo(TIMED_WAITING);94thread.checkThreadState(TIMED_WAITING);95checkLockInfo(thread, TIMED_WAITING,96globalLock, null);979899thread.transitionToPark(true /* timed park */);100thread.checkThreadState(TIMED_WAITING);101checkLockInfo(thread, TIMED_WAITING, null, null);102103thread.transitionToPark(false /* indefinite park */);104thread.checkThreadState(WAITING);105checkLockInfo(thread, WAITING, null, null);106107thread.transitionToSleep();108thread.checkThreadState(TIMED_WAITING);109checkLockInfo(thread, TIMED_WAITING, null, null);110111thread.transitionTo(TERMINATED);112thread.checkThreadState(TERMINATED);113114try {115System.out.println(thread.getLog());116} catch (InterruptedException e) {117e.printStackTrace();118System.out.println("TEST FAILED: Unexpected exception.");119throw new RuntimeException(e);120}121System.out.println("Test passed.");122}123124private static void checkSuspendedThreadState(ThreadStateController t, Thread.State state) {125ThreadInfo info = getThreadInfo(t, state);126if (info == null) {127throw new RuntimeException(t.getName() +128" expected to have ThreadInfo " +129" but got null.");130}131132if (info.getThreadState() != state) {133throw new RuntimeException(t.getName() + " expected to be in " +134state + " state but got " + info.getThreadState());135}136137if (!info.isSuspended()) {138throw new RuntimeException(t.getName() + " expected to be suspended " +139" but isSuspended() returns " + info.isSuspended());140}141}142143private static String getLockName(Object lock) {144if (lock == null) return null;145146return lock.getClass().getName() + '@' +147Integer.toHexString(System.identityHashCode(lock));148}149150// maximum number of retries when checking for thread state.151private static final int MAX_RETRY = 500;152private static ThreadInfo getThreadInfo(ThreadStateController t, Thread.State expected) {153// wait for the thread to transition to the expected state.154// There is a small window between the thread checking the state155// and the thread actual entering that state.156int retryCount=0;157ThreadInfo info = tm.getThreadInfo(t.getId());158while (info.getThreadState() != expected && retryCount < MAX_RETRY) {159ThreadStateController.pause(10);160retryCount++;161info = tm.getThreadInfo(t.getId());162}163return info;164}165166private static void checkLockInfo(ThreadStateController t, Thread.State state,167Object lock, Thread owner) {168ThreadInfo info = getThreadInfo(t, state);169if (info == null) {170throw new RuntimeException(t.getName() +171" expected to have ThreadInfo " +172" but got null.");173}174175if (info.getThreadState() != state) {176throw new RuntimeException(t.getName() + " expected to be in " +177state + " state but got " + info.getThreadState());178}179180if (lock == null && info.getLockName() != null) {181throw new RuntimeException(t.getName() +182" expected not to be blocked on any lock" +183" but got " + info.getLockName());184}185String expectedLockName = getLockName(lock);186if (lock != null && info.getLockName() == null) {187throw new RuntimeException(t.getName() +188" expected to be blocked on lock [" + expectedLockName +189"] but got null.");190}191192if (lock != null && !expectedLockName.equals(info.getLockName())) {193throw new RuntimeException(t.getName() +194" expected to be blocked on lock [" + expectedLockName +195"] but got [" + info.getLockName() + "].");196}197198if (owner == null && info.getLockOwnerName() != null) {199throw new RuntimeException("Lock owner is expected " +200" to be null but got " + info.getLockOwnerName());201}202203if (owner != null && info.getLockOwnerName() == null) {204throw new RuntimeException("Lock owner is expected to be " +205owner.getName() +206" but got null.");207}208if (owner != null && !info.getLockOwnerName().equals(owner.getName())) {209throw new RuntimeException("Lock owner is expected to be " +210owner.getName() +211" but got " + owner.getName());212}213if (owner == null && info.getLockOwnerId() != -1) {214throw new RuntimeException("Lock owner is expected " +215" to be -1 but got " + info.getLockOwnerId());216}217218if (owner != null && info.getLockOwnerId() <= 0) {219throw new RuntimeException("Lock owner is expected to be " +220owner.getName() + "(id = " + owner.getId() +221") but got " + info.getLockOwnerId());222}223if (owner != null && info.getLockOwnerId() != owner.getId()) {224throw new RuntimeException("Lock owner is expected to be " +225owner.getName() + "(id = " + owner.getId() +226") but got " + info.getLockOwnerId());227}228if (info.isSuspended()) {229throw new RuntimeException(t.getName() +230" isSuspended() returns " + info.isSuspended());231}232}233}234235236