1 /* 2 * Copyright (c) 2004, 2011, 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 /* 25 * @test 26 * @bug 5014783 8022208 27 * @summary Basic unit test of thread states returned by 28 * Thread.getState(). 29 * 30 * @author Mandy Chung 31 * @library /lib/testlibrary 32 * @build ThreadStateTest ThreadStateController 33 * @run main ThreadStateTest 34 */ 35 36 public class ThreadStateTest { 37 private static boolean testFailed = false; 38 39 // used to achieve waiting states 40 private static final Object globalLock = new Object(); 41 42 public static void main(String[] argv) { 43 // Call Thread.getState to force all initialization done 44 // before test verification begins. 45 Thread.currentThread().getState(); 46 ThreadStateController myThread = new ThreadStateController("MyThread", globalLock); 47 myThread.setDaemon(true); 48 49 // before myThread starts 50 ThreadStateController.checkThreadState(myThread, Thread.State.NEW); 51 52 myThread.start(); 53 myThread.waitUntilStarted(); 54 ThreadStateController.checkThreadState(myThread, Thread.State.RUNNABLE); 55 56 synchronized (globalLock) { 57 myThread.goBlocked(); 58 ThreadStateController.checkThreadState(myThread, Thread.State.BLOCKED); 59 } 60 61 myThread.goWaiting(); 62 ThreadStateController.checkThreadState(myThread, Thread.State.WAITING); 63 64 myThread.goTimedWaiting(); 65 ThreadStateController.checkThreadState(myThread, Thread.State.TIMED_WAITING); 66 67 68 /* 69 *********** park and parkUntil seems not working 70 * ignore this case for now. 71 * Bug ID 5062095 72 *********************************************** 73 74 myThread.goParked(); 75 ThreadStateController.checkThreadState(myThread, Thread.State.WAITING); 76 77 myThread.goTimedParked(); 78 ThreadStateController.checkThreadState(myThread, Thread.State.TIMED_WAITING); 79 */ 80 81 82 myThread.goSleeping(); 83 ThreadStateController.checkThreadState(myThread, Thread.State.TIMED_WAITING); 84 85 myThread.terminate(); 86 ThreadStateController.checkThreadState(myThread, Thread.State.TERMINATED); 87 88 try { 89 myThread.join(); 90 } catch (InterruptedException e) { 91 e.printStackTrace(); 92 System.out.println("Unexpected exception."); 93 testFailed = true; 94 } 95 96 if (testFailed) 97 throw new RuntimeException("TEST FAILED."); 98 System.out.println("Test passed."); 99 } 100 }