duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
lana | f3d11f6 | 2013-12-26 12:04:16 -0800 | [diff] [blame] | 2 | * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 24 | import static java.lang.Thread.State.*; |
| 25 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 26 | /* |
| 27 | * @test |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 28 | * @bug 5014783 8022208 |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 29 | * @summary Basic unit test of thread states returned by |
| 30 | * Thread.getState(). |
| 31 | * |
| 32 | * @author Mandy Chung |
jbachorik | d6aa6ce | 2014-11-05 09:49:20 +0100 | [diff] [blame^] | 33 | * @library /lib/testlibrary |
| 34 | * @build jdk.testlibrary.* |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 35 | * @build ThreadStateTest ThreadStateController |
morris | 3296464 | 2013-10-11 12:40:14 -0700 | [diff] [blame] | 36 | * @run main/othervm -Xmixed ThreadStateTest |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 39 | public class ThreadStateTest { |
| 40 | private static boolean testFailed = false; |
| 41 | |
chegar | 8c26e26 | 2011-06-23 13:15:14 +0100 | [diff] [blame] | 42 | // used to achieve waiting states |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 43 | private static final Object globalLock = new Object(); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 44 | |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 45 | public static void main(String[] argv) throws Exception { |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 46 | // Call Thread.getState to force all initialization done |
| 47 | // before test verification begins. |
| 48 | Thread.currentThread().getState(); |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 49 | ThreadStateController thread = new ThreadStateController("StateChanger", globalLock); |
| 50 | thread.setDaemon(true); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 51 | |
| 52 | // before myThread starts |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 53 | thread.checkThreadState(NEW); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 54 | |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 55 | thread.start(); |
| 56 | thread.transitionTo(RUNNABLE); |
| 57 | thread.checkThreadState(RUNNABLE); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 58 | |
| 59 | synchronized (globalLock) { |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 60 | thread.transitionTo(BLOCKED); |
| 61 | thread.checkThreadState(BLOCKED); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 62 | } |
| 63 | |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 64 | thread.transitionTo(WAITING); |
| 65 | thread.checkThreadState(WAITING); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 66 | |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 67 | thread.transitionTo(TIMED_WAITING); |
| 68 | thread.checkThreadState(TIMED_WAITING); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 69 | |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 70 | thread.transitionToPark(true /* timed park*/); |
| 71 | thread.checkThreadState(TIMED_WAITING); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 72 | |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 73 | thread.transitionToPark(false /* indefinite park */); |
| 74 | thread.checkThreadState(WAITING); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 75 | |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 76 | thread.transitionToSleep(); |
| 77 | thread.checkThreadState(TIMED_WAITING); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 78 | |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 79 | thread.transitionTo(TERMINATED); |
| 80 | thread.checkThreadState(TERMINATED); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 81 | |
| 82 | try { |
mchung | d70ecd4 | 2013-11-05 17:33:26 -0800 | [diff] [blame] | 83 | thread.join(); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 84 | } catch (InterruptedException e) { |
| 85 | e.printStackTrace(); |
| 86 | System.out.println("Unexpected exception."); |
| 87 | testFailed = true; |
| 88 | } |
chegar | 8c26e26 | 2011-06-23 13:15:14 +0100 | [diff] [blame] | 89 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 90 | if (testFailed) |
| 91 | throw new RuntimeException("TEST FAILED."); |
| 92 | System.out.println("Test passed."); |
| 93 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 94 | } |