1 /*
   2  * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   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  * @library /lib/testlibrary
  34  * @library /test/lib
  35  * @build jdk.testlibrary.*
  36  * @build jdk.test.lib.LockFreeLogger
  37  * @build ThreadStateTest ThreadStateController
  38  * @run main/othervm -Xmixed ThreadStateTest
  39  */
  40 
  41 public class ThreadStateTest {
  42     private static boolean testFailed = false;
  43 
  44     // used to achieve waiting states
  45     private static final Object globalLock = new Object();
  46 
  47     public static void main(String[] argv) throws Exception {
  48         // Call Thread.getState to force all initialization done
  49         // before test verification begins.
  50         Thread.currentThread().getState();
  51         ThreadStateController thread = new ThreadStateController("StateChanger", globalLock);
  52         thread.setDaemon(true);
  53 
  54         // before myThread starts
  55         thread.checkThreadState(NEW);
  56 
  57         thread.start();
  58         thread.transitionTo(RUNNABLE);
  59         thread.checkThreadState(RUNNABLE);
  60 
  61         synchronized (globalLock) {
  62             thread.transitionTo(BLOCKED);
  63             thread.checkThreadState(BLOCKED);
  64         }
  65 
  66         thread.transitionTo(WAITING);
  67         thread.checkThreadState(WAITING);
  68 
  69         thread.transitionTo(TIMED_WAITING);
  70         thread.checkThreadState(TIMED_WAITING);
  71 
  72         thread.transitionToPark(true /* timed park*/);
  73         thread.checkThreadState(TIMED_WAITING);
  74 
  75         thread.transitionToPark(false /* indefinite park */);
  76         thread.checkThreadState(WAITING);
  77 
  78         thread.transitionToSleep();
  79         thread.checkThreadState(TIMED_WAITING);
  80 
  81         thread.transitionTo(TERMINATED);
  82         thread.checkThreadState(TERMINATED);
  83 
  84         try {
  85             thread.join();
  86         } catch (InterruptedException e) {
  87             e.printStackTrace();
  88             System.out.println("Unexpected exception.");
  89             testFailed = true;
  90         }
  91 
  92         if (testFailed)
  93             throw new RuntimeException("TEST FAILED.");
  94         System.out.println("Test passed.");
  95     }
  96 }