--- old/test/java/lang/management/ThreadMXBean/ThreadStateTest.java Thu Oct 31 11:53:01 2013 +++ new/test/java/lang/management/ThreadMXBean/ThreadStateTest.java Thu Oct 31 11:53:00 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,8 @@ * * @author Mandy Chung * - * @build ThreadExecutionSynchronizer Utils + * @library /lib/testlibrary + * @build ThreadStateTest ThreadStateController * @run main ThreadStateTest */ @@ -38,8 +39,6 @@ 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(); @@ -59,14 +58,15 @@ // verification begins. Thread.currentThread().getState(); - MyThread myThread = new MyThread("MyThread"); + ThreadStateController myThread = new ThreadStateController("MyThread", globalLock); + myThread.setDaemon(true); // before myThread starts - // Utils.checkThreadState(myThread, Thread.State.NEW); + ThreadStateController.checkThreadState(myThread, Thread.State.NEW); myThread.start(); myThread.waitUntilStarted(); - Utils.checkThreadState(myThread, Thread.State.RUNNABLE); + ThreadStateController.checkThreadState(myThread, Thread.State.RUNNABLE); checkLockInfo(myThread, Thread.State.RUNNABLE, null, null); myThread.suspend(); @@ -76,18 +76,18 @@ synchronized (globalLock) { myThread.goBlocked(); - Utils.checkThreadState(myThread, Thread.State.BLOCKED); + ThreadStateController.checkThreadState(myThread, Thread.State.BLOCKED); checkLockInfo(myThread, Thread.State.BLOCKED, globalLock, Thread.currentThread()); } myThread.goWaiting(); - Utils.checkThreadState(myThread, Thread.State.WAITING); + ThreadStateController.checkThreadState(myThread, Thread.State.WAITING); checkLockInfo(myThread, Thread.State.WAITING, globalLock, null); myThread.goTimedWaiting(); - Utils.checkThreadState(myThread, Thread.State.TIMED_WAITING); + ThreadStateController.checkThreadState(myThread, Thread.State.TIMED_WAITING); checkLockInfo(myThread, Thread.State.TIMED_WAITING, globalLock, null); @@ -100,22 +100,22 @@ Bug ID : 5062095 *********************************************** myThread.goParked(); - Utils.checkThreadState(myThread, Thread.State.WAITING); + ThreadStateController.checkThreadState(myThread, Thread.State.WAITING); checkLockInfo(myThread, Thread.State.WAITING, null, null); myThread.goTimedParked(); - Utils.checkThreadState(myThread, Thread.State.TIMED_WAITING); + ThreadStateController.checkThreadState(myThread, Thread.State.TIMED_WAITING); checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null); */ myThread.goSleeping(); - Utils.checkThreadState(myThread, Thread.State.TIMED_WAITING); + ThreadStateController.checkThreadState(myThread, Thread.State.TIMED_WAITING); checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null); myThread.terminate(); - // Utils.checkThreadState(myThread, ThreadState.TERMINATED); + // ThreadStateController.checkThreadState(myThread, ThreadState.TERMINATED); try { myThread.join(); @@ -127,7 +127,7 @@ System.out.println("Test passed."); } - private static void checkSuspendedThreadState(Thread t, Thread.State state) { + private static void checkSuspendedThreadState(ThreadStateController t, Thread.State state) { ThreadInfo info = tm.getThreadInfo(t.getId()); if (info == null) { throw new RuntimeException(t.getName() + @@ -144,7 +144,7 @@ throw new RuntimeException(t.getName() + " expected to be suspended " + " but isSuspended() returns " + info.isSuspended()); } - Utils.checkThreadState(t, state); + ThreadStateController.checkThreadState(t, state); } private static String getLockName(Object lock) { @@ -220,205 +220,4 @@ " isSuspended() returns " + info.isSuspended()); } } - - 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."); - 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 - 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 - interrupted(); - } - 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: - while (state == BLOCKED) { - Utils.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; - } - } - } }