ThreadStateTest.java revision 9330:8b1f1c2a400f
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
24import 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/othervm -Xmixed ThreadStateTest
35 */
36
37public 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}
93