test/java/lang/management/ThreadMXBean/ThreadMXBeanStateTest.java

Print this page

        

@@ -21,28 +21,28 @@
  * 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
- * @run main ThreadStateTest
+ * @library ../../Thread
+ * @build ThreadMXBeanStateTest ThreadStateController
+ * @run main ThreadMXBeanStateTest
  */
 
 import java.lang.management.ManagementFactory;
 import java.lang.management.ThreadMXBean;
 import java.lang.management.ThreadInfo;
+import static java.lang.Thread.State.*;
 
-import java.util.concurrent.locks.LockSupport;
-
-public class ThreadStateTest {
+public class ThreadMXBeanStateTest {
     private static final ThreadMXBean tm = ManagementFactory.getThreadMXBean();
 
     static class Lock {
         private String name;
         Lock(String name) {

@@ -52,75 +52,66 @@
             return name;
         }
     }
     private static Lock globalLock = new Lock("my lock");
 
-    public static void main(String[] argv) {
+    public static void main(String[] argv) throws Exception {
         // Force thread state initialization now before the test
         // verification begins.
         Thread.currentThread().getState();
+        ThreadStateController thread = new ThreadStateController("StateChanger", globalLock);
+        thread.setDaemon(true);
 
-        MyThread myThread = new MyThread("MyThread");
-
         // before myThread starts
-        // Utils.checkThreadState(myThread, Thread.State.NEW);
+        thread.checkThreadState(NEW);
 
-        myThread.start();
-        myThread.waitUntilStarted();
-        Utils.checkThreadState(myThread, Thread.State.RUNNABLE);
-        checkLockInfo(myThread, Thread.State.RUNNABLE, null, null);
+        thread.start();
+        thread.transitionTo(RUNNABLE);
+        thread.checkThreadState(RUNNABLE);
+        checkLockInfo(thread, RUNNABLE, null, null);
 
-        myThread.suspend();
-        Utils.goSleep(10);
-        checkSuspendedThreadState(myThread, Thread.State.RUNNABLE);
-        myThread.resume();
+        thread.suspend();
+        thread.goSleep(10);
+        thread.checkThreadState(RUNNABLE);
+        checkSuspendedThreadState(thread, RUNNABLE);
+        thread.resume();
 
         synchronized (globalLock) {
-            myThread.goBlocked();
-            Utils.checkThreadState(myThread, Thread.State.BLOCKED);
-            checkLockInfo(myThread, Thread.State.BLOCKED,
+            thread.transitionTo(BLOCKED);
+            thread.checkThreadState(BLOCKED);
+            checkLockInfo(thread, BLOCKED,
                           globalLock, Thread.currentThread());
         }
 
-        myThread.goWaiting();
-        Utils.checkThreadState(myThread, Thread.State.WAITING);
-        checkLockInfo(myThread, Thread.State.WAITING,
+        thread.transitionTo(WAITING);
+        thread.checkThreadState(WAITING);
+        checkLockInfo(thread, Thread.State.WAITING,
                       globalLock, null);
 
-        myThread.goTimedWaiting();
-        Utils.checkThreadState(myThread, Thread.State.TIMED_WAITING);
-        checkLockInfo(myThread, Thread.State.TIMED_WAITING,
+        thread.transitionTo(TIMED_WAITING);
+        thread.checkThreadState(TIMED_WAITING);
+        checkLockInfo(thread, TIMED_WAITING,
                       globalLock, null);
 
 
+        thread.transitionToPark(true /* timed park */);
+        thread.checkThreadState(TIMED_WAITING);
+        checkLockInfo(thread, TIMED_WAITING, null, null);
 
-      /*
-       *********** parkUntil seems not working
-       * ignore this park case for now.
+        thread.transitionToPark(false /* indefinite park */);
+        thread.checkThreadState(WAITING);
+        checkLockInfo(thread, WAITING, null, null);
 
-         Bug ID : 5062095
-       ***********************************************
-        myThread.goParked();
-        Utils.checkThreadState(myThread, Thread.State.WAITING);
-        checkLockInfo(myThread, Thread.State.WAITING, null, null);
+        thread.transitionToSleep();
+        thread.checkThreadState(TIMED_WAITING);
+        checkLockInfo(thread, TIMED_WAITING, null, null);
 
-        myThread.goTimedParked();
-        Utils.checkThreadState(myThread, Thread.State.TIMED_WAITING);
-        checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null);
+        thread.transitionTo(TERMINATED);
+        thread.checkThreadState(TERMINATED);
 
-       */
-
-        myThread.goSleeping();
-        Utils.checkThreadState(myThread, Thread.State.TIMED_WAITING);
-        checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null);
-
-
-        myThread.terminate();
-        // Utils.checkThreadState(myThread, ThreadState.TERMINATED);
-
         try {
-            myThread.join();
+            thread.join();
         } catch (InterruptedException e) {
             e.printStackTrace();
             System.out.println("TEST FAILED: Unexpected exception.");
             throw new RuntimeException(e);
         }

@@ -142,11 +133,10 @@
 
         if (!info.isSuspended()) {
             throw new RuntimeException(t.getName() + " expected to be suspended " +
                 " but isSuspended() returns " + info.isSuspended());
         }
-        Utils.checkThreadState(t, state);
     }
 
     private static String getLockName(Object lock) {
         if (lock == null) return null;
 

@@ -218,207 +208,6 @@
         if (info.isSuspended()) {
             throw new RuntimeException(t.getName() +
                 " 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;
-            }
-        }
-    }
 }