--- 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; } } }