test/java/lang/Thread/ThreadStateTest.java

Print this page




   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 


  24 /*
  25  * @test
  26  * @bug     5014783
  27  * @summary Basic unit test of thread states returned by
  28  *          Thread.getState().
  29  *
  30  * @author  Mandy Chung
  31  *
  32  * @build ThreadStateTest
  33  * @run main ThreadStateTest
  34  */
  35 
  36 import java.util.concurrent.locks.LockSupport;
  37 import java.util.concurrent.Phaser;
  38 
  39 public class ThreadStateTest {
  40     // maximum number of retries when checking for thread state.
  41     static final int MAX_RETRY = 500;
  42 
  43     private static boolean testFailed = false;
  44 
  45     // used to achieve waiting states
  46     static final Object globalLock = new Object();
  47 
  48     public static void main(String[] argv) {
  49         // Call Thread.getState to force all initialization done
  50         // before test verification begins.
  51         Thread.currentThread().getState();
  52         MyThread myThread = new MyThread("MyThread");

  53 
  54         // before myThread starts
  55         checkThreadState(myThread, Thread.State.NEW);
  56 
  57         myThread.start();
  58         myThread.waitUntilStarted();
  59         checkThreadState(myThread, Thread.State.RUNNABLE);
  60 
  61         synchronized (globalLock) {
  62             myThread.goBlocked();
  63             checkThreadState(myThread, Thread.State.BLOCKED);
  64         }
  65 
  66         myThread.goWaiting();
  67         checkThreadState(myThread, Thread.State.WAITING);
  68 
  69         myThread.goTimedWaiting();
  70         checkThreadState(myThread, Thread.State.TIMED_WAITING);
  71 


  72 
  73       /*
  74        *********** park and parkUntil seems not working
  75        * ignore this case for now.
  76        * Bug ID 5062095
  77        ***********************************************
  78 
  79         myThread.goParked();
  80         checkThreadState(myThread, Thread.State.WAITING);
  81 
  82         myThread.goTimedParked();
  83         checkThreadState(myThread, Thread.State.TIMED_WAITING);
  84        */
  85 
  86 
  87         myThread.goSleeping();
  88         checkThreadState(myThread, Thread.State.TIMED_WAITING);
  89 
  90         myThread.terminate();
  91         checkThreadState(myThread, Thread.State.TERMINATED);
  92 
  93         try {
  94             myThread.join();
  95         } catch (InterruptedException e) {
  96             e.printStackTrace();
  97             System.out.println("Unexpected exception.");
  98             testFailed = true;
  99         }
 100 
 101         if (testFailed)
 102             throw new RuntimeException("TEST FAILED.");
 103         System.out.println("Test passed.");
 104     }
 105 
 106     private static void checkThreadState(Thread t, Thread.State expected) {
 107         // wait for the thread to transition to the expected state.
 108         // There is a small window between the thread checking the state
 109         // and the thread actual entering that state.
 110         Thread.State state;
 111         int retryCount=0;
 112         while ((state = t.getState()) != expected && retryCount < MAX_RETRY) {
 113             if (state != Thread.State.RUNNABLE) {
 114                 throw new RuntimeException("Thread not in expected state yet," +
 115                         " but it should at least be RUNNABLE");
 116             }
 117             goSleep(10);
 118             retryCount++;
 119         }
 120 
 121         System.out.println("Checking thread state " + state);
 122         if (state == null) {
 123             throw new RuntimeException(t.getName() + " expected to have " +
 124                 expected + " but got null.");
 125         }
 126 
 127         if (state != expected) {
 128             throw new RuntimeException(t.getName() + " expected to have " +
 129                 expected + " but got " + state);
 130         }
 131     }
 132 
 133     private static void goSleep(long ms) {
 134         try {
 135             Thread.sleep(ms);
 136         } catch (InterruptedException e) {
 137             e.printStackTrace();
 138             System.out.println("Unexpected exception.");
 139             testFailed = true;
 140         }
 141     }
 142 
 143     static class MyThread extends Thread {
 144         // Phaser to sync between the main thread putting
 145         // this thread into various states
 146         private Phaser phaser =  new Phaser(2);
 147 
 148         MyThread(String name) {
 149             super(name);
 150         }
 151 
 152         private final int RUNNABLE = 0;
 153         private final int BLOCKED = 1;
 154         private final int WAITING = 2;
 155         private final int TIMED_WAITING = 3;
 156         private final int PARKED = 4;
 157         private final int TIMED_PARKED = 5;
 158         private final int SLEEPING = 6;
 159         private final int TERMINATE = 7;
 160 
 161         private volatile int state = RUNNABLE;
 162 
 163         private boolean done = false;
 164         public void run() {
 165             // Signal main thread to continue.
 166             phaser.arriveAndAwaitAdvance();
 167 
 168             while (!done) {
 169                 switch (state) {
 170                     case RUNNABLE: {
 171                         double sum = 0;
 172                         for (int i = 0; i < 1000; i++) {
 173                            double r = Math.random();
 174                            double x = Math.pow(3, r);
 175                            sum += x - r;
 176                         }
 177                         break;
 178                     }
 179                     case BLOCKED: {
 180                         // signal main thread.
 181                         phaser.arrive();
 182                         System.out.println("  myThread is going to block.");
 183                         synchronized (globalLock) {
 184                             // finish blocking
 185                             state = RUNNABLE;
 186                         }
 187                         break;
 188                     }
 189                     case WAITING: {
 190                         synchronized (globalLock) {
 191                             // signal main thread.
 192                             phaser.arrive();
 193                             System.out.println("  myThread is going to wait.");
 194                             try {
 195                                 globalLock.wait();
 196                             } catch (InterruptedException e) {
 197                                 // ignore
 198                             }
 199                         }
 200                         break;
 201                     }
 202                     case TIMED_WAITING: {
 203                         synchronized (globalLock) {
 204                             // signal main thread.
 205                             phaser.arrive();
 206                             System.out.println("  myThread is going to timed wait.");
 207                             try {
 208                                 globalLock.wait(10000);
 209                             } catch (InterruptedException e) {
 210                                 // ignore
 211                             }
 212                         }
 213                         break;
 214                     }
 215                     case PARKED: {
 216                         // signal main thread.
 217                         phaser.arrive();
 218                         System.out.println("  myThread is going to park.");
 219                         LockSupport.park();
 220                         // give a chance for the main thread to block
 221                         goSleep(10);
 222                         break;
 223                     }
 224                     case TIMED_PARKED: {
 225                         // signal main thread.
 226                         phaser.arrive();
 227                         System.out.println("  myThread is going to timed park.");
 228                         long deadline = System.currentTimeMillis() + 10000*1000;
 229                         LockSupport.parkUntil(deadline);
 230 
 231                         // give a chance for the main thread to block
 232                         goSleep(10);
 233                         break;
 234                     }
 235                     case SLEEPING: {
 236                         // signal main thread.
 237                         phaser.arrive();
 238                         System.out.println("  myThread is going to sleep.");
 239                         try {
 240                             Thread.sleep(1000000);
 241                         } catch (InterruptedException e) {
 242                             // finish sleeping
 243                         }
 244                         break;
 245                     }
 246                     case TERMINATE: {
 247                         done = true;
 248                         // signal main thread.
 249                         phaser.arrive();
 250                         break;
 251                     }
 252                     default:
 253                         break;
 254                 }
 255             }
 256         }
 257 
 258         public void waitUntilStarted() {
 259             // wait for MyThread.
 260             phaser.arriveAndAwaitAdvance();
 261         }
 262 
 263         public void goBlocked() {
 264             System.out.println("Waiting myThread to go blocked.");
 265             setState(BLOCKED);
 266             // wait for MyThread to get to a point just before being blocked
 267             phaser.arriveAndAwaitAdvance();
 268         }
 269 
 270         public void goWaiting() {
 271             System.out.println("Waiting myThread to go waiting.");
 272             setState(WAITING);
 273             // wait for MyThread to get to just before wait on object.
 274             phaser.arriveAndAwaitAdvance();
 275         }
 276 
 277         public void goTimedWaiting() {
 278             System.out.println("Waiting myThread to go timed waiting.");
 279             setState(TIMED_WAITING);
 280             // wait for MyThread to get to just before timed wait call.
 281             phaser.arriveAndAwaitAdvance();
 282         }
 283 
 284         public void goParked() {
 285             System.out.println("Waiting myThread to go parked.");
 286             setState(PARKED);
 287             // wait for MyThread to get to just before parked.
 288             phaser.arriveAndAwaitAdvance();
 289         }
 290 
 291         public void goTimedParked() {
 292             System.out.println("Waiting myThread to go timed parked.");
 293             setState(TIMED_PARKED);
 294             // wait for MyThread to get to just before timed park.
 295             phaser.arriveAndAwaitAdvance();
 296         }
 297 
 298         public void goSleeping() {
 299             System.out.println("Waiting myThread to go sleeping.");
 300             setState(SLEEPING);
 301             // wait for MyThread to get to just before sleeping
 302             phaser.arriveAndAwaitAdvance();
 303         }
 304 
 305         public void terminate() {
 306             System.out.println("Waiting myThread to terminate.");
 307             setState(TERMINATE);
 308             // wait for MyThread to get to just before terminate
 309             phaser.arriveAndAwaitAdvance();
 310         }
 311 
 312         private void setState(int newState) {
 313             switch (state) {
 314                 case BLOCKED:
 315                     while (state == BLOCKED) {
 316                         goSleep(10);
 317                     }
 318                     state = newState;
 319                     break;
 320                 case WAITING:
 321                 case TIMED_WAITING:
 322                     state = newState;
 323                     synchronized (globalLock) {
 324                         globalLock.notify();
 325                     }
 326                     break;
 327                 case PARKED:
 328                 case TIMED_PARKED:
 329                     state = newState;
 330                     LockSupport.unpark(this);
 331                     break;
 332                 case SLEEPING:
 333                     state = newState;
 334                     this.interrupt();
 335                     break;
 336                 default:
 337                     state = newState;
 338                     break;
 339             }
 340         }
 341     }
 342 }


   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import static java.lang.Thread.State.*;
  25 
  26 /*
  27  * @test
  28  * @bug     5014783 8022208
  29  * @summary Basic unit test of thread states returned by
  30  *          Thread.getState().
  31  *
  32  * @author  Mandy Chung
  33  * @build ThreadStateTest ThreadStateController

  34  * @run main ThreadStateTest
  35  */
  36 



  37 public class ThreadStateTest {



  38     private static boolean testFailed = false;
  39 
  40     // used to achieve waiting states
  41     private static final Object globalLock = new Object();
  42 
  43     public static void main(String[] argv) throws Exception {
  44         // Call Thread.getState to force all initialization done
  45         // before test verification begins.
  46         Thread.currentThread().getState();
  47         ThreadStateController thread = new ThreadStateController("StateChanger", globalLock);
  48         thread.setDaemon(true);
  49 
  50         // before myThread starts
  51         thread.checkThreadState(NEW);
  52 
  53         thread.start();
  54         thread.transitionTo(RUNNABLE);
  55         thread.checkThreadState(RUNNABLE);
  56 
  57         synchronized (globalLock) {
  58             thread.transitionTo(BLOCKED);
  59             thread.checkThreadState(BLOCKED);
  60         }
  61 
  62         thread.transitionTo(WAITING);
  63         thread.checkThreadState(WAITING);
  64 
  65         thread.transitionTo(TIMED_WAITING);
  66         thread.checkThreadState(TIMED_WAITING);
  67 
  68         thread.transitionToPark(true /* timed park*/);
  69         thread.checkThreadState(TIMED_WAITING);
  70 
  71         thread.transitionToPark(false /* indefinite park */);
  72         thread.checkThreadState(WAITING);



  73 
  74         thread.transitionToSleep();
  75         thread.checkThreadState(TIMED_WAITING);
  76 
  77         thread.transitionTo(TERMINATED);
  78         thread.checkThreadState(TERMINATED);

  79 







  80         try {
  81             thread.join();
  82         } catch (InterruptedException e) {
  83             e.printStackTrace();
  84             System.out.println("Unexpected exception.");
  85             testFailed = true;
  86         }
  87 
  88         if (testFailed)
  89             throw new RuntimeException("TEST FAILED.");
  90         System.out.println("Test passed.");
  91     }













































































































































































































































  92 }