test/java/lang/management/ThreadMXBean/ThreadStateTest.java
Print this page
@@ -21,25 +21,25 @@
* questions.
*/
/*
* @test
- * @bug 4967283 5080203
+ * @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 ThreadExecutionSynchronizer Utils
+ * @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,10 +58,11 @@
// 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,11 +221,13 @@
" isSuspended() returns " + info.isSuspended());
}
}
static class MyThread extends Thread {
- private ThreadExecutionSynchronizer thrsync = new ThreadExecutionSynchronizer();
+ // 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,16 +237,16 @@
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 volatile int state = RUNNABLE;
private boolean done = false;
public void run() {
// Signal main thread to continue.
- thrsync.signal();
+ phaser.arriveAndAwaitAdvance();
+
while (!done) {
switch (state) {
case RUNNABLE: {
double sum = 0;
for (int i = 0; i < 1000; i++) {
@@ -253,11 +256,11 @@
}
break;
}
case BLOCKED: {
// signal main thread.
- thrsync.signal();
+ phaser.arrive();
System.out.println(" myThread is going to block.");
synchronized (globalLock) {
// finish blocking
state = RUNNABLE;
}
@@ -264,11 +267,11 @@
break;
}
case WAITING: {
synchronized (globalLock) {
// signal main thread.
- thrsync.signal();
+ phaser.arrive();
System.out.println(" myThread is going to wait.");
try {
globalLock.wait();
} catch (InterruptedException e) {
// ignore
@@ -277,11 +280,11 @@
break;
}
case TIMED_WAITING: {
synchronized (globalLock) {
// signal main thread.
- thrsync.signal();
+ phaser.arrive();
System.out.println(" myThread is going to timed wait.");
try {
globalLock.wait(10000);
} catch (InterruptedException e) {
// ignore
@@ -289,21 +292,20 @@
}
break;
}
case PARKED: {
// signal main thread.
- thrsync.signal();
+ phaser.arrive();
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();
+ 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,11 +312,11 @@
Utils.goSleep(10);
break;
}
case SLEEPING: {
// signal main thread.
- thrsync.signal();
+ phaser.arrive();
System.out.println(" myThread is going to sleep.");
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
// finish sleeping
@@ -323,74 +325,71 @@
break;
}
case TERMINATE: {
done = true;
// signal main thread.
- thrsync.signal();
+ phaser.arrive();
break;
}
default:
break;
}
}
}
+
public void waitUntilStarted() {
// wait for MyThread.
- thrsync.waitForSignal();
- Utils.goSleep(10);
+ phaser.arriveAndAwaitAdvance();
}
public void goBlocked() {
System.out.println("Waiting myThread to go blocked.");
setState(BLOCKED);
- // wait for MyThread to get blocked
- thrsync.waitForSignal();
- Utils.goSleep(20);
+ // 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 wait on object.
- thrsync.waitForSignal();
- Utils.goSleep(20);
+ // 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 timed wait call.
- thrsync.waitForSignal();
- Utils.goSleep(20);
+ // 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 state change to PARKED.
- thrsync.waitForSignal();
- Utils.goSleep(20);
+ // 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.
- thrsync.waitForSignal();
- Utils.goSleep(20);
+ // 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.
- thrsync.waitForSignal();
- Utils.goSleep(20);
+ // 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.
- thrsync.waitForSignal();
- Utils.goSleep(20);
+ // wait for MyThread to get to just before terminate
+ phaser.arriveAndAwaitAdvance();
}
private void setState(int newState) {
switch (state) {
case BLOCKED: