test/java/lang/management/ThreadMXBean/ThreadStateTest.java
Print this page
*** 21,45 ****
* questions.
*/
/*
* @test
! * @bug 4967283 5080203
* @summary Basic unit test of thread states returned by
* ThreadMXBean.getThreadInfo.getThreadState().
* It also tests lock information returned by ThreadInfo.
*
* @author Mandy Chung
*
! * @build ThreadExecutionSynchronizer Utils
* @run main ThreadStateTest
*/
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.lang.management.ThreadInfo;
!
import java.util.concurrent.locks.LockSupport;
public class ThreadStateTest {
private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean();
--- 21,45 ----
* questions.
*/
/*
* @test
! * @bug 4967283 5080203 8022208
* @summary Basic unit test of thread states returned by
* ThreadMXBean.getThreadInfo.getThreadState().
* It also tests lock information returned by ThreadInfo.
*
* @author Mandy Chung
*
! * @build Utils
* @run main ThreadStateTest
*/
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.lang.management.ThreadInfo;
! import java.util.concurrent.Phaser;
import java.util.concurrent.locks.LockSupport;
public class ThreadStateTest {
private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean();
*** 58,67 ****
--- 58,68 ----
// Force thread state initialization now before the test
// verification begins.
Thread.currentThread().getState();
MyThread myThread = new MyThread("MyThread");
+ myThread.setDaemon(true);
// before myThread starts
// Utils.checkThreadState(myThread, Thread.State.NEW);
myThread.start();
*** 220,230 ****
" isSuspended() returns " + info.isSuspended());
}
}
static class MyThread extends Thread {
! private ThreadExecutionSynchronizer thrsync = new ThreadExecutionSynchronizer();
MyThread(String name) {
super(name);
}
--- 221,233 ----
" isSuspended() returns " + info.isSuspended());
}
}
static class MyThread extends Thread {
! // Phaser to sync between the main thread putting
! // this thread into various states
! private Phaser phaser = new Phaser(2);
MyThread(String name) {
super(name);
}
*** 234,249 ****
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++) {
--- 237,252 ----
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 volatile int state = RUNNABLE;
private boolean done = false;
public void run() {
// Signal main thread to continue.
! phaser.arriveAndAwaitAdvance();
!
while (!done) {
switch (state) {
case RUNNABLE: {
double sum = 0;
for (int i = 0; i < 1000; i++) {
*** 253,263 ****
}
break;
}
case BLOCKED: {
// signal main thread.
! thrsync.signal();
System.out.println(" myThread is going to block.");
synchronized (globalLock) {
// finish blocking
state = RUNNABLE;
}
--- 256,266 ----
}
break;
}
case BLOCKED: {
// signal main thread.
! phaser.arrive();
System.out.println(" myThread is going to block.");
synchronized (globalLock) {
// finish blocking
state = RUNNABLE;
}
*** 264,274 ****
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
--- 267,277 ----
break;
}
case WAITING: {
synchronized (globalLock) {
// signal main thread.
! phaser.arrive();
System.out.println(" myThread is going to wait.");
try {
globalLock.wait();
} catch (InterruptedException e) {
// ignore
*** 277,287 ****
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
--- 280,290 ----
break;
}
case TIMED_WAITING: {
synchronized (globalLock) {
// signal main thread.
! phaser.arrive();
System.out.println(" myThread is going to timed wait.");
try {
globalLock.wait(10000);
} catch (InterruptedException e) {
// ignore
*** 289,309 ****
}
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.");
Utils.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
--- 292,311 ----
}
break;
}
case PARKED: {
// signal main thread.
! phaser.arrive();
System.out.println(" myThread is going to park.");
LockSupport.park();
// give a chance for the main thread to block
Utils.goSleep(10);
break;
}
case TIMED_PARKED: {
// signal main thread.
! phaser.arrive();
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
*** 310,320 ****
Utils.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
--- 312,322 ----
Utils.goSleep(10);
break;
}
case SLEEPING: {
// signal main thread.
! phaser.arrive();
System.out.println(" myThread is going to sleep.");
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
// finish sleeping
*** 323,396 ****
break;
}
case TERMINATE: {
done = true;
// signal main thread.
! thrsync.signal();
break;
}
default:
break;
}
}
}
public void waitUntilStarted() {
// wait for MyThread.
! thrsync.waitForSignal();
! Utils.goSleep(10);
}
public void goBlocked() {
System.out.println("Waiting myThread to go blocked.");
setState(BLOCKED);
! // wait for MyThread to get blocked
! thrsync.waitForSignal();
! Utils.goSleep(20);
}
public void goWaiting() {
System.out.println("Waiting myThread to go waiting.");
setState(WAITING);
! // wait for MyThread to wait on object.
! thrsync.waitForSignal();
! Utils.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();
! Utils.goSleep(20);
}
public void goParked() {
System.out.println("Waiting myThread to go parked.");
setState(PARKED);
! // wait for MyThread state change to PARKED.
! thrsync.waitForSignal();
! Utils.goSleep(20);
}
public void goTimedParked() {
System.out.println("Waiting myThread to go timed parked.");
setState(TIMED_PARKED);
! // wait for MyThread.
! thrsync.waitForSignal();
! Utils.goSleep(20);
}
public void goSleeping() {
System.out.println("Waiting myThread to go sleeping.");
setState(SLEEPING);
! // wait for MyThread.
! thrsync.waitForSignal();
! Utils.goSleep(20);
}
public void terminate() {
System.out.println("Waiting myThread to terminate.");
setState(TERMINATE);
! // wait for MyThread.
! thrsync.waitForSignal();
! Utils.goSleep(20);
}
private void setState(int newState) {
switch (state) {
case BLOCKED:
--- 325,395 ----
break;
}
case TERMINATE: {
done = true;
// signal main thread.
! phaser.arrive();
break;
}
default:
break;
}
}
}
+
public void waitUntilStarted() {
// wait for MyThread.
! phaser.arriveAndAwaitAdvance();
}
public void goBlocked() {
System.out.println("Waiting myThread to go blocked.");
setState(BLOCKED);
! // wait for MyThread to get to a point just before being blocked
! phaser.arriveAndAwaitAdvance();
}
public void goWaiting() {
System.out.println("Waiting myThread to go waiting.");
setState(WAITING);
! // wait for MyThread to get to just before wait on object.
! phaser.arriveAndAwaitAdvance();
}
+
public void goTimedWaiting() {
System.out.println("Waiting myThread to go timed waiting.");
setState(TIMED_WAITING);
! // wait for MyThread to get to just before timed wait call.
! phaser.arriveAndAwaitAdvance();
}
+
public void goParked() {
System.out.println("Waiting myThread to go parked.");
setState(PARKED);
! // wait for MyThread to get to just before parked.
! phaser.arriveAndAwaitAdvance();
}
+
public void goTimedParked() {
System.out.println("Waiting myThread to go timed parked.");
setState(TIMED_PARKED);
! // wait for MyThread to get to just before timed park.
! phaser.arriveAndAwaitAdvance();
}
public void goSleeping() {
System.out.println("Waiting myThread to go sleeping.");
setState(SLEEPING);
! // wait for MyThread to get to just before sleeping
! phaser.arriveAndAwaitAdvance();
}
+
public void terminate() {
System.out.println("Waiting myThread to terminate.");
setState(TERMINATE);
! // wait for MyThread to get to just before terminate
! phaser.arriveAndAwaitAdvance();
}
private void setState(int newState) {
switch (state) {
case BLOCKED: