ThreadStateTest.java revision 1153
3867N/A/*
3867N/A * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
3867N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3867N/A *
3867N/A * This code is free software; you can redistribute it and/or modify it
3867N/A * under the terms of the GNU General Public License version 2 only, as
3867N/A * published by the Free Software Foundation.
3867N/A *
3867N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3867N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3867N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3867N/A * version 2 for more details (a copy is included in the LICENSE file that
3867N/A * accompanied this code).
3867N/A *
3867N/A * You should have received a copy of the GNU General Public License version
3867N/A * 2 along with this work; if not, write to the Free Software Foundation,
3867N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3867N/A *
3867N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3867N/A * CA 95054 USA or visit www.sun.com if you need additional information or
3867N/A * have any questions.
3867N/A */
3867N/A
3867N/A/*
3867N/A * @test
3867N/A * @bug 4967283 5080203
3867N/A * @ignore Due to 5080203, cannot rely on this test always passing.
3867N/A * @summary Basic unit test of thread states returned by
3867N/A * ThreadMXBean.getThreadInfo.getThreadState().
3867N/A * It also tests lock information returned by ThreadInfo.
3867N/A *
3867N/A * @author Mandy Chung
3867N/A *
3867N/A * @build ThreadExecutionSynchronizer
3867N/A * @run main ThreadStateTest
3867N/A */
3867N/A
3867N/Aimport java.lang.management.ManagementFactory;
3867N/Aimport java.lang.management.ThreadMXBean;
3867N/Aimport java.lang.management.ThreadInfo;
3867N/A
3867N/Aimport java.util.concurrent.locks.LockSupport;
3867N/A
3867N/Apublic class ThreadStateTest {
3867N/A private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean();
3867N/A private static boolean testFailed = false;
3867N/A
3867N/A static class Lock {
3867N/A private String name;
3867N/A Lock(String name) {
3867N/A this.name = name;
3867N/A }
3867N/A public String toString() {
3867N/A return name;
3867N/A }
4254N/A }
3867N/A private static Lock globalLock = new Lock("my lock");
3867N/A
3867N/A public static void main(String[] argv) {
3867N/A // Force thread state initialization now before the test
3867N/A // verification begins.
4254N/A Thread.currentThread().getState();
4254N/A
4254N/A MyThread myThread = new MyThread("MyThread");
4254N/A
4254N/A // before myThread starts
4254N/A // checkThreadState(myThread, Thread.State.NEW);
4254N/A
4254N/A myThread.start();
4254N/A myThread.waitUntilStarted();
3867N/A checkThreadState(myThread, Thread.State.RUNNABLE);
3867N/A checkLockInfo(myThread, Thread.State.RUNNABLE, null, null);
3867N/A
3867N/A myThread.suspend();
3867N/A goSleep(10);
3867N/A checkSuspendedThreadState(myThread, Thread.State.RUNNABLE);
4254N/A myThread.resume();
4254N/A
4254N/A synchronized (globalLock) {
3867N/A myThread.goBlocked();
3867N/A checkThreadState(myThread, Thread.State.BLOCKED);
3867N/A checkLockInfo(myThread, Thread.State.BLOCKED,
3867N/A globalLock, Thread.currentThread());
3867N/A }
3867N/A
3867N/A myThread.goWaiting();
3867N/A checkThreadState(myThread, Thread.State.WAITING);
3867N/A checkLockInfo(myThread, Thread.State.WAITING,
3867N/A globalLock, null);
3867N/A
3867N/A myThread.goTimedWaiting();
4032N/A checkThreadState(myThread, Thread.State.TIMED_WAITING);
3867N/A checkLockInfo(myThread, Thread.State.TIMED_WAITING,
3867N/A globalLock, null);
4032N/A
3867N/A
3867N/A
3867N/A /*
3867N/A *********** parkUntil seems not working
3867N/A * ignore this park case for now.
3867N/A
3867N/A Bug ID : 5062095
3867N/A ***********************************************
3867N/A myThread.goParked();
3867N/A checkThreadState(myThread, Thread.State.WAITING);
3867N/A checkLockInfo(myThread, Thread.State.WAITING, null, null);
3867N/A
3867N/A myThread.goTimedParked();
3867N/A checkThreadState(myThread, Thread.State.TIMED_WAITING);
3867N/A checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null);
3867N/A
3867N/A */
3867N/A
3867N/A myThread.goSleeping();
3867N/A checkThreadState(myThread, Thread.State.TIMED_WAITING);
3867N/A checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null);
3867N/A
3867N/A
3867N/A myThread.terminate();
3867N/A // checkThreadState(myThread, ThreadState.TERMINATED);
3867N/A
3867N/A try {
3867N/A myThread.join();
3867N/A } catch (InterruptedException e) {
3867N/A e.printStackTrace();
3867N/A System.out.println("Unexpected exception.");
3867N/A testFailed = true;
3867N/A }
3867N/A if (testFailed)
3867N/A throw new RuntimeException("TEST FAILED.");
3867N/A System.out.println("Test passed.");
4254N/A }
4254N/A
4254N/A private static void checkSuspendedThreadState(Thread t, Thread.State state) {
3867N/A ThreadInfo info = tm.getThreadInfo(t.getId());
3867N/A if (info == null) {
3867N/A throw new RuntimeException(t.getName() +
3867N/A " expected to have ThreadInfo " +
3867N/A " but got null.");
3867N/A }
3867N/A
3867N/A if (info.getThreadState() != state) {
3867N/A throw new RuntimeException(t.getName() + " expected to be in " +
3867N/A state + " state but got " + info.getThreadState());
3867N/A }
3867N/A
3867N/A if (!info.isSuspended()) {
3867N/A throw new RuntimeException(t.getName() + " expected to be suspended " +
3867N/A " but isSuspended() returns " + info.isSuspended());
3884N/A }
3867N/A checkThreadState(t, state);
3867N/A }
3884N/A
3867N/A private static void checkThreadState(Thread t, Thread.State expected) {
3867N/A ThreadInfo ti = tm.getThreadInfo(t.getId());
4254N/A Thread.State state = ti.getThreadState();
4254N/A if (state == null) {
4254N/A throw new RuntimeException(t.getName() + " expected to have " +
3867N/A expected + " but got null.");
3867N/A }
3867N/A
3867N/A if (state != expected) {
3867N/A if (expected == Thread.State.BLOCKED) {
3867N/A int retryCount=0;
3867N/A while (ti.getThreadState() != expected) {
3867N/A if (retryCount >= 500) {
3867N/A throw new RuntimeException(t.getName() +
3867N/A " expected to have " + expected + " but got " + state);
3867N/A }
3867N/A goSleep(100);
3867N/A }
3867N/A } else {
3867N/A throw new RuntimeException(t.getName() + " expected to have " +
3867N/A expected + " but got " + state);
3867N/A }
3867N/A }
3867N/A }
3867N/A
3867N/A private static String getLockName(Object lock) {
3867N/A if (lock == null) return null;
3867N/A
3867N/A return lock.getClass().getName() + '@' +
3867N/A Integer.toHexString(System.identityHashCode(lock));
3867N/A }
3867N/A
3867N/A private static void checkLockInfo(Thread t, Thread.State state, Object lock, Thread owner) {
3867N/A ThreadInfo info = tm.getThreadInfo(t.getId());
3867N/A if (info == null) {
3867N/A throw new RuntimeException(t.getName() +
3867N/A " expected to have ThreadInfo " +
3867N/A " but got null.");
3867N/A }
3867N/A
3867N/A if (info.getThreadState() != state) {
3867N/A throw new RuntimeException(t.getName() + " expected to be in " +
3867N/A state + " state but got " + info.getThreadState());
3867N/A }
3867N/A
3867N/A if (lock == null && info.getLockName() != null) {
3867N/A throw new RuntimeException(t.getName() +
3867N/A " expected not to be blocked on any lock" +
3867N/A " but got " + info.getLockName());
3867N/A }
3867N/A String expectedLockName = getLockName(lock);
3867N/A if (lock != null && info.getLockName() == null) {
3867N/A throw new RuntimeException(t.getName() +
3867N/A " expected to be blocked on lock [" + expectedLockName +
3867N/A "] but got null.");
3867N/A }
3867N/A
3867N/A if (lock != null && !expectedLockName.equals(info.getLockName())) {
3867N/A throw new RuntimeException(t.getName() +
3867N/A " expected to be blocked on lock [" + expectedLockName +
3867N/A "] but got [" + info.getLockName() + "].");
3867N/A }
3867N/A
3867N/A if (owner == null && info.getLockOwnerName() != null) {
3867N/A throw new RuntimeException("Lock owner is expected " +
3867N/A " to be null but got " + info.getLockOwnerName());
3867N/A }
3867N/A
3867N/A if (owner != null && info.getLockOwnerName() == null) {
3867N/A throw new RuntimeException("Lock owner is expected to be " +
3867N/A owner.getName() +
3867N/A " but got null.");
3867N/A }
3867N/A if (owner != null && !info.getLockOwnerName().equals(owner.getName())) {
3867N/A throw new RuntimeException("Lock owner is expected to be " +
3867N/A owner.getName() +
3867N/A " but got " + owner.getName());
}
if (owner == null && info.getLockOwnerId() != -1) {
throw new RuntimeException("Lock owner is expected " +
" to be -1 but got " + info.getLockOwnerId());
}
if (owner != null && info.getLockOwnerId() <= 0) {
throw new RuntimeException("Lock owner is expected to be " +
owner.getName() + "(id = " + owner.getId() +
") but got " + info.getLockOwnerId());
}
if (owner != null && info.getLockOwnerId() != owner.getId()) {
throw new RuntimeException("Lock owner is expected to be " +
owner.getName() + "(id = " + owner.getId() +
") but got " + info.getLockOwnerId());
}
if (info.isSuspended()) {
throw new RuntimeException(t.getName() +
" isSuspended() returns " + info.isSuspended());
}
}
private static void goSleep(long ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Unexpected exception.");
testFailed = true;
}
}
static class MyThread extends Thread {
private ThreadExecutionSynchronizer thrsync = new ThreadExecutionSynchronizer();
MyThread(String name) {
super(name);
}
private final int RUNNABLE = 0;
private final int BLOCKED = 1;
private final int WAITING = 2;
private final int TIMED_WAITING = 3;
private final int PARKED = 4;
private final int TIMED_PARKED = 5;
private final int SLEEPING = 6;
private final int TERMINATE = 7;
private int state = RUNNABLE;
private boolean done = false;
public void run() {
// Signal main thread to continue.
thrsync.signal();
while (!done) {
switch (state) {
case RUNNABLE: {
double sum = 0;
for (int i = 0; i < 1000; i++) {
double r = Math.random();
double x = Math.pow(3, r);
sum += x - r;
}
break;
}
case BLOCKED: {
// signal main thread.
thrsync.signal();
System.out.println(" myThread is going to block.");
synchronized (globalLock) {
// finish blocking
state = RUNNABLE;
}
break;
}
case WAITING: {
synchronized (globalLock) {
// signal main thread.
thrsync.signal();
System.out.println(" myThread is going to wait.");
try {
globalLock.wait();
} catch (InterruptedException e) {
// ignore
}
}
break;
}
case TIMED_WAITING: {
synchronized (globalLock) {
// signal main thread.
thrsync.signal();
System.out.println(" myThread is going to timed wait.");
try {
globalLock.wait(10000);
} catch (InterruptedException e) {
// ignore
}
}
break;
}
case PARKED: {
// signal main thread.
thrsync.signal();
System.out.println(" myThread is going to park.");
LockSupport.park();
// give a chance for the main thread to block
System.out.println(" myThread is going to park.");
goSleep(10);
break;
}
case TIMED_PARKED: {
// signal main thread.
thrsync.signal();
System.out.println(" myThread is going to timed park.");
long deadline = System.currentTimeMillis() + 10000*1000;
LockSupport.parkUntil(deadline);
// give a chance for the main thread to block
goSleep(10);
break;
}
case SLEEPING: {
// signal main thread.
thrsync.signal();
System.out.println(" myThread is going to sleep.");
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
// finish sleeping
interrupted();
}
break;
}
case TERMINATE: {
done = true;
// signal main thread.
thrsync.signal();
break;
}
default:
break;
}
}
}
public void waitUntilStarted() {
// wait for MyThread.
thrsync.waitForSignal();
goSleep(10);
}
public void goBlocked() {
System.out.println("Waiting myThread to go blocked.");
setState(BLOCKED);
// wait for MyThread to get blocked
thrsync.waitForSignal();
goSleep(20);
}
public void goWaiting() {
System.out.println("Waiting myThread to go waiting.");
setState(WAITING);
// wait for MyThread to wait on object.
thrsync.waitForSignal();
goSleep(20);
}
public void goTimedWaiting() {
System.out.println("Waiting myThread to go timed waiting.");
setState(TIMED_WAITING);
// wait for MyThread timed wait call.
thrsync.waitForSignal();
goSleep(20);
}
public void goParked() {
System.out.println("Waiting myThread to go parked.");
setState(PARKED);
// wait for MyThread state change to PARKED.
thrsync.waitForSignal();
goSleep(20);
}
public void goTimedParked() {
System.out.println("Waiting myThread to go timed parked.");
setState(TIMED_PARKED);
// wait for MyThread.
thrsync.waitForSignal();
goSleep(20);
}
public void goSleeping() {
System.out.println("Waiting myThread to go sleeping.");
setState(SLEEPING);
// wait for MyThread.
thrsync.waitForSignal();
goSleep(20);
}
public void terminate() {
System.out.println("Waiting myThread to terminate.");
setState(TERMINATE);
// wait for MyThread.
thrsync.waitForSignal();
goSleep(20);
}
private void setState(int newState) {
switch (state) {
case BLOCKED:
while (state == BLOCKED) {
goSleep(20);
}
state = newState;
break;
case WAITING:
case TIMED_WAITING:
state = newState;
synchronized (globalLock) {
globalLock.notify();
}
break;
case PARKED:
case TIMED_PARKED:
state = newState;
LockSupport.unpark(this);
break;
case SLEEPING:
state = newState;
this.interrupt();
break;
default:
state = newState;
break;
}
}
}
}