blob: 843cfd7ddebbfa23657661f943df6ade8193f318 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
lanaf3d11f62013-12-26 12:04:16 -08002 * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
duke6e45e102007-12-01 00:00:00 +000022 */
23
mchungd70ecd42013-11-05 17:33:26 -080024import static java.lang.Thread.State.*;
25
duke6e45e102007-12-01 00:00:00 +000026/*
27 * @test
mchungd70ecd42013-11-05 17:33:26 -080028 * @bug 5014783 8022208
duke6e45e102007-12-01 00:00:00 +000029 * @summary Basic unit test of thread states returned by
30 * Thread.getState().
31 *
32 * @author Mandy Chung
jbachorikd6aa6ce2014-11-05 09:49:20 +010033 * @library /lib/testlibrary
34 * @build jdk.testlibrary.*
mchungd70ecd42013-11-05 17:33:26 -080035 * @build ThreadStateTest ThreadStateController
morris32964642013-10-11 12:40:14 -070036 * @run main/othervm -Xmixed ThreadStateTest
duke6e45e102007-12-01 00:00:00 +000037 */
38
duke6e45e102007-12-01 00:00:00 +000039public class ThreadStateTest {
40 private static boolean testFailed = false;
41
chegar8c26e262011-06-23 13:15:14 +010042 // used to achieve waiting states
mchungd70ecd42013-11-05 17:33:26 -080043 private static final Object globalLock = new Object();
duke6e45e102007-12-01 00:00:00 +000044
mchungd70ecd42013-11-05 17:33:26 -080045 public static void main(String[] argv) throws Exception {
duke6e45e102007-12-01 00:00:00 +000046 // Call Thread.getState to force all initialization done
47 // before test verification begins.
48 Thread.currentThread().getState();
mchungd70ecd42013-11-05 17:33:26 -080049 ThreadStateController thread = new ThreadStateController("StateChanger", globalLock);
50 thread.setDaemon(true);
duke6e45e102007-12-01 00:00:00 +000051
52 // before myThread starts
mchungd70ecd42013-11-05 17:33:26 -080053 thread.checkThreadState(NEW);
duke6e45e102007-12-01 00:00:00 +000054
mchungd70ecd42013-11-05 17:33:26 -080055 thread.start();
56 thread.transitionTo(RUNNABLE);
57 thread.checkThreadState(RUNNABLE);
duke6e45e102007-12-01 00:00:00 +000058
59 synchronized (globalLock) {
mchungd70ecd42013-11-05 17:33:26 -080060 thread.transitionTo(BLOCKED);
61 thread.checkThreadState(BLOCKED);
duke6e45e102007-12-01 00:00:00 +000062 }
63
mchungd70ecd42013-11-05 17:33:26 -080064 thread.transitionTo(WAITING);
65 thread.checkThreadState(WAITING);
duke6e45e102007-12-01 00:00:00 +000066
mchungd70ecd42013-11-05 17:33:26 -080067 thread.transitionTo(TIMED_WAITING);
68 thread.checkThreadState(TIMED_WAITING);
duke6e45e102007-12-01 00:00:00 +000069
mchungd70ecd42013-11-05 17:33:26 -080070 thread.transitionToPark(true /* timed park*/);
71 thread.checkThreadState(TIMED_WAITING);
duke6e45e102007-12-01 00:00:00 +000072
mchungd70ecd42013-11-05 17:33:26 -080073 thread.transitionToPark(false /* indefinite park */);
74 thread.checkThreadState(WAITING);
duke6e45e102007-12-01 00:00:00 +000075
mchungd70ecd42013-11-05 17:33:26 -080076 thread.transitionToSleep();
77 thread.checkThreadState(TIMED_WAITING);
duke6e45e102007-12-01 00:00:00 +000078
mchungd70ecd42013-11-05 17:33:26 -080079 thread.transitionTo(TERMINATED);
80 thread.checkThreadState(TERMINATED);
duke6e45e102007-12-01 00:00:00 +000081
82 try {
mchungd70ecd42013-11-05 17:33:26 -080083 thread.join();
duke6e45e102007-12-01 00:00:00 +000084 } catch (InterruptedException e) {
85 e.printStackTrace();
86 System.out.println("Unexpected exception.");
87 testFailed = true;
88 }
chegar8c26e262011-06-23 13:15:14 +010089
duke6e45e102007-12-01 00:00:00 +000090 if (testFailed)
91 throw new RuntimeException("TEST FAILED.");
92 System.out.println("Test passed.");
93 }
duke6e45e102007-12-01 00:00:00 +000094}