--- old/test/java/lang/Thread/ThreadStateTest.java Thu Oct 31 10:42:25 2013 +++ new/test/java/lang/Thread/ThreadStateTest.java Thu Oct 31 10:42:24 2013 @@ -23,7 +23,7 @@ /* * @test - * @bug 5014783 + * @bug 5014783 8022208 * @summary Basic unit test of thread states returned by * Thread.getState(). * @@ -38,7 +38,7 @@ public class ThreadStateTest { // maximum number of retries when checking for thread state. - static final int MAX_RETRY = 500; + private static final int MAX_RETRY = 500; private static boolean testFailed = false; @@ -50,6 +50,7 @@ // before test verification begins. Thread.currentThread().getState(); MyThread myThread = new MyThread("MyThread"); + myThread.setDaemon(true); // before myThread starts checkThreadState(myThread, Thread.State.NEW); @@ -103,7 +104,7 @@ System.out.println("Test passed."); } - private static void checkThreadState(Thread t, Thread.State expected) { + private static void checkThreadState(MyThread t, Thread.State expected) { // wait for the thread to transition to the expected state. // There is a small window between the thread checking the state // and the thread actual entering that state. @@ -110,15 +111,10 @@ Thread.State state; int retryCount=0; while ((state = t.getState()) != expected && retryCount < MAX_RETRY) { - if (state != Thread.State.RUNNABLE) { - throw new RuntimeException("Thread not in expected state yet," + - " but it should at least be RUNNABLE"); - } goSleep(10); retryCount++; } - System.out.println("Checking thread state " + state); if (state == null) { throw new RuntimeException(t.getName() + " expected to have " + expected + " but got null."); @@ -125,8 +121,9 @@ } if (state != expected) { - throw new RuntimeException(t.getName() + " expected to have " + - expected + " but got " + state); + throw new RuntimeException(String.format("%s expected in %s state but got %s " + + "(iterations %d interrupted %d)%n", + t.getName(), expected, state, t.iterations, t.interrupted)); } } @@ -161,11 +158,14 @@ private volatile int state = RUNNABLE; private boolean done = false; + volatile int iterations=0; + volatile int interrupted=0; public void run() { // Signal main thread to continue. phaser.arriveAndAwaitAdvance(); while (!done) { + iterations++; switch (state) { case RUNNABLE: { double sum = 0; @@ -195,6 +195,7 @@ globalLock.wait(); } catch (InterruptedException e) { // ignore + interrupted++; } } break; @@ -208,6 +209,7 @@ globalLock.wait(10000); } catch (InterruptedException e) { // ignore + interrupted++; } } break; @@ -337,6 +339,8 @@ state = newState; break; } + iterations=0; + interrupted=0; } } } --- old/test/java/lang/management/ThreadMXBean/ThreadStateTest.java Thu Oct 31 10:42:26 2013 +++ new/test/java/lang/management/ThreadMXBean/ThreadStateTest.java Thu Oct 31 10:42:25 2013 @@ -23,7 +23,7 @@ /* * @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. @@ -30,7 +30,7 @@ * * @author Mandy Chung * - * @build ThreadExecutionSynchronizer Utils + * @build Utils * @run main ThreadStateTest */ @@ -37,7 +37,7 @@ 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 { @@ -60,6 +60,7 @@ Thread.currentThread().getState(); MyThread myThread = new MyThread("MyThread"); + myThread.setDaemon(true); // before myThread starts // Utils.checkThreadState(myThread, Thread.State.NEW); @@ -222,7 +223,9 @@ } 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); @@ -236,12 +239,12 @@ 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: { @@ -255,7 +258,7 @@ } case BLOCKED: { // signal main thread. - thrsync.signal(); + phaser.arrive(); System.out.println(" myThread is going to block."); synchronized (globalLock) { // finish blocking @@ -266,7 +269,7 @@ case WAITING: { synchronized (globalLock) { // signal main thread. - thrsync.signal(); + phaser.arrive(); System.out.println(" myThread is going to wait."); try { globalLock.wait(); @@ -279,7 +282,7 @@ 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); @@ -291,17 +294,16 @@ } 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); @@ -312,7 +314,7 @@ } case SLEEPING: { // signal main thread. - thrsync.signal(); + phaser.arrive(); System.out.println(" myThread is going to sleep."); try { Thread.sleep(1000000); @@ -325,7 +327,7 @@ case TERMINATE: { done = true; // signal main thread. - thrsync.signal(); + phaser.arrive(); break; } default: @@ -333,62 +335,59 @@ } } } + 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) {