blob: c4fa18850b2a240b434e0a4119c9eb06f2eea97f [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
chegar8c26e262011-06-23 13:15:14 +01002 * Copyright (c) 2004, 2011, 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
24/*
25 * @test
26 * @bug 5014783
27 * @summary Basic unit test of thread states returned by
28 * Thread.getState().
29 *
30 * @author Mandy Chung
31 *
32 * @build ThreadStateTest
33 * @run main ThreadStateTest
34 */
35
36import java.util.concurrent.locks.LockSupport;
chegar8c26e262011-06-23 13:15:14 +010037import java.util.concurrent.Phaser;
duke6e45e102007-12-01 00:00:00 +000038
39public class ThreadStateTest {
chegar8c26e262011-06-23 13:15:14 +010040 // maximum number of retries when checking for thread state.
41 static final int MAX_RETRY = 500;
42
duke6e45e102007-12-01 00:00:00 +000043 private static boolean testFailed = false;
44
chegar8c26e262011-06-23 13:15:14 +010045 // used to achieve waiting states
46 static final Object globalLock = new Object();
duke6e45e102007-12-01 00:00:00 +000047
48 public static void main(String[] argv) {
49 // Call Thread.getState to force all initialization done
50 // before test verification begins.
51 Thread.currentThread().getState();
52 MyThread myThread = new MyThread("MyThread");
53
54 // before myThread starts
55 checkThreadState(myThread, Thread.State.NEW);
56
57 myThread.start();
58 myThread.waitUntilStarted();
59 checkThreadState(myThread, Thread.State.RUNNABLE);
60
61 synchronized (globalLock) {
62 myThread.goBlocked();
63 checkThreadState(myThread, Thread.State.BLOCKED);
64 }
65
66 myThread.goWaiting();
67 checkThreadState(myThread, Thread.State.WAITING);
68
69 myThread.goTimedWaiting();
70 checkThreadState(myThread, Thread.State.TIMED_WAITING);
71
72
73 /*
74 *********** park and parkUntil seems not working
75 * ignore this case for now.
76 * Bug ID 5062095
77 ***********************************************
78
79 myThread.goParked();
80 checkThreadState(myThread, Thread.State.WAITING);
81
82 myThread.goTimedParked();
83 checkThreadState(myThread, Thread.State.TIMED_WAITING);
84 */
85
86
87 myThread.goSleeping();
88 checkThreadState(myThread, Thread.State.TIMED_WAITING);
89
90 myThread.terminate();
91 checkThreadState(myThread, Thread.State.TERMINATED);
92
93 try {
94 myThread.join();
95 } catch (InterruptedException e) {
96 e.printStackTrace();
97 System.out.println("Unexpected exception.");
98 testFailed = true;
99 }
chegar8c26e262011-06-23 13:15:14 +0100100
duke6e45e102007-12-01 00:00:00 +0000101 if (testFailed)
102 throw new RuntimeException("TEST FAILED.");
103 System.out.println("Test passed.");
104 }
105
106 private static void checkThreadState(Thread t, Thread.State expected) {
chegar8c26e262011-06-23 13:15:14 +0100107 // wait for the thread to transition to the expected state.
108 // There is a small window between the thread checking the state
109 // and the thread actual entering that state.
110 Thread.State state;
111 int retryCount=0;
112 while ((state = t.getState()) != expected && retryCount < MAX_RETRY) {
113 if (state != Thread.State.RUNNABLE) {
114 throw new RuntimeException("Thread not in expected state yet," +
115 " but it should at least be RUNNABLE");
116 }
117 goSleep(10);
118 retryCount++;
119 }
120
duke6e45e102007-12-01 00:00:00 +0000121 System.out.println("Checking thread state " + state);
122 if (state == null) {
123 throw new RuntimeException(t.getName() + " expected to have " +
124 expected + " but got null.");
125 }
126
127 if (state != expected) {
128 throw new RuntimeException(t.getName() + " expected to have " +
129 expected + " but got " + state);
130 }
131 }
132
duke6e45e102007-12-01 00:00:00 +0000133 private static void goSleep(long ms) {
134 try {
135 Thread.sleep(ms);
136 } catch (InterruptedException e) {
137 e.printStackTrace();
138 System.out.println("Unexpected exception.");
139 testFailed = true;
140 }
141 }
142
143 static class MyThread extends Thread {
chegar8c26e262011-06-23 13:15:14 +0100144 // Phaser to sync between the main thread putting
145 // this thread into various states
146 private Phaser phaser = new Phaser(2);
duke6e45e102007-12-01 00:00:00 +0000147
148 MyThread(String name) {
149 super(name);
150 }
151
152 private final int RUNNABLE = 0;
153 private final int BLOCKED = 1;
154 private final int WAITING = 2;
155 private final int TIMED_WAITING = 3;
156 private final int PARKED = 4;
157 private final int TIMED_PARKED = 5;
158 private final int SLEEPING = 6;
159 private final int TERMINATE = 7;
chegar8c26e262011-06-23 13:15:14 +0100160
161 private volatile int state = RUNNABLE;
duke6e45e102007-12-01 00:00:00 +0000162
163 private boolean done = false;
164 public void run() {
165 // Signal main thread to continue.
chegar8c26e262011-06-23 13:15:14 +0100166 phaser.arriveAndAwaitAdvance();
167
duke6e45e102007-12-01 00:00:00 +0000168 while (!done) {
169 switch (state) {
170 case RUNNABLE: {
171 double sum = 0;
172 for (int i = 0; i < 1000; i++) {
173 double r = Math.random();
174 double x = Math.pow(3, r);
175 sum += x - r;
176 }
177 break;
178 }
179 case BLOCKED: {
180 // signal main thread.
chegar8c26e262011-06-23 13:15:14 +0100181 phaser.arrive();
duke6e45e102007-12-01 00:00:00 +0000182 System.out.println(" myThread is going to block.");
183 synchronized (globalLock) {
184 // finish blocking
185 state = RUNNABLE;
186 }
187 break;
188 }
189 case WAITING: {
190 synchronized (globalLock) {
191 // signal main thread.
chegar8c26e262011-06-23 13:15:14 +0100192 phaser.arrive();
duke6e45e102007-12-01 00:00:00 +0000193 System.out.println(" myThread is going to wait.");
194 try {
195 globalLock.wait();
196 } catch (InterruptedException e) {
197 // ignore
198 }
199 }
200 break;
201 }
202 case TIMED_WAITING: {
203 synchronized (globalLock) {
204 // signal main thread.
chegar8c26e262011-06-23 13:15:14 +0100205 phaser.arrive();
duke6e45e102007-12-01 00:00:00 +0000206 System.out.println(" myThread is going to timed wait.");
207 try {
208 globalLock.wait(10000);
209 } catch (InterruptedException e) {
210 // ignore
211 }
212 }
213 break;
214 }
215 case PARKED: {
216 // signal main thread.
chegar8c26e262011-06-23 13:15:14 +0100217 phaser.arrive();
duke6e45e102007-12-01 00:00:00 +0000218 System.out.println(" myThread is going to park.");
219 LockSupport.park();
220 // give a chance for the main thread to block
221 goSleep(10);
222 break;
223 }
224 case TIMED_PARKED: {
225 // signal main thread.
chegar8c26e262011-06-23 13:15:14 +0100226 phaser.arrive();
duke6e45e102007-12-01 00:00:00 +0000227 System.out.println(" myThread is going to timed park.");
228 long deadline = System.currentTimeMillis() + 10000*1000;
229 LockSupport.parkUntil(deadline);
230
231 // give a chance for the main thread to block
232 goSleep(10);
233 break;
234 }
235 case SLEEPING: {
236 // signal main thread.
chegar8c26e262011-06-23 13:15:14 +0100237 phaser.arrive();
duke6e45e102007-12-01 00:00:00 +0000238 System.out.println(" myThread is going to sleep.");
239 try {
240 Thread.sleep(1000000);
241 } catch (InterruptedException e) {
242 // finish sleeping
duke6e45e102007-12-01 00:00:00 +0000243 }
244 break;
245 }
246 case TERMINATE: {
247 done = true;
248 // signal main thread.
chegar8c26e262011-06-23 13:15:14 +0100249 phaser.arrive();
duke6e45e102007-12-01 00:00:00 +0000250 break;
251 }
252 default:
253 break;
254 }
255 }
256 }
chegar8c26e262011-06-23 13:15:14 +0100257
duke6e45e102007-12-01 00:00:00 +0000258 public void waitUntilStarted() {
259 // wait for MyThread.
chegar8c26e262011-06-23 13:15:14 +0100260 phaser.arriveAndAwaitAdvance();
duke6e45e102007-12-01 00:00:00 +0000261 }
262
263 public void goBlocked() {
264 System.out.println("Waiting myThread to go blocked.");
265 setState(BLOCKED);
chegar8c26e262011-06-23 13:15:14 +0100266 // wait for MyThread to get to a point just before being blocked
267 phaser.arriveAndAwaitAdvance();
duke6e45e102007-12-01 00:00:00 +0000268 }
269
270 public void goWaiting() {
271 System.out.println("Waiting myThread to go waiting.");
272 setState(WAITING);
chegar8c26e262011-06-23 13:15:14 +0100273 // wait for MyThread to get to just before wait on object.
274 phaser.arriveAndAwaitAdvance();
duke6e45e102007-12-01 00:00:00 +0000275 }
chegar8c26e262011-06-23 13:15:14 +0100276
duke6e45e102007-12-01 00:00:00 +0000277 public void goTimedWaiting() {
278 System.out.println("Waiting myThread to go timed waiting.");
279 setState(TIMED_WAITING);
chegar8c26e262011-06-23 13:15:14 +0100280 // wait for MyThread to get to just before timed wait call.
281 phaser.arriveAndAwaitAdvance();
duke6e45e102007-12-01 00:00:00 +0000282 }
chegar8c26e262011-06-23 13:15:14 +0100283
duke6e45e102007-12-01 00:00:00 +0000284 public void goParked() {
285 System.out.println("Waiting myThread to go parked.");
286 setState(PARKED);
chegar8c26e262011-06-23 13:15:14 +0100287 // wait for MyThread to get to just before parked.
288 phaser.arriveAndAwaitAdvance();
duke6e45e102007-12-01 00:00:00 +0000289 }
chegar8c26e262011-06-23 13:15:14 +0100290
duke6e45e102007-12-01 00:00:00 +0000291 public void goTimedParked() {
292 System.out.println("Waiting myThread to go timed parked.");
293 setState(TIMED_PARKED);
chegar8c26e262011-06-23 13:15:14 +0100294 // wait for MyThread to get to just before timed park.
295 phaser.arriveAndAwaitAdvance();
duke6e45e102007-12-01 00:00:00 +0000296 }
297
298 public void goSleeping() {
299 System.out.println("Waiting myThread to go sleeping.");
300 setState(SLEEPING);
chegar8c26e262011-06-23 13:15:14 +0100301 // wait for MyThread to get to just before sleeping
302 phaser.arriveAndAwaitAdvance();
duke6e45e102007-12-01 00:00:00 +0000303 }
chegar8c26e262011-06-23 13:15:14 +0100304
duke6e45e102007-12-01 00:00:00 +0000305 public void terminate() {
306 System.out.println("Waiting myThread to terminate.");
307 setState(TERMINATE);
chegar8c26e262011-06-23 13:15:14 +0100308 // wait for MyThread to get to just before terminate
309 phaser.arriveAndAwaitAdvance();
duke6e45e102007-12-01 00:00:00 +0000310 }
311
312 private void setState(int newState) {
313 switch (state) {
314 case BLOCKED:
315 while (state == BLOCKED) {
chegar8c26e262011-06-23 13:15:14 +0100316 goSleep(10);
duke6e45e102007-12-01 00:00:00 +0000317 }
318 state = newState;
319 break;
320 case WAITING:
321 case TIMED_WAITING:
322 state = newState;
323 synchronized (globalLock) {
324 globalLock.notify();
325 }
326 break;
327 case PARKED:
328 case TIMED_PARKED:
329 state = newState;
330 LockSupport.unpark(this);
331 break;
332 case SLEEPING:
333 state = newState;
334 this.interrupt();
335 break;
336 default:
337 state = newState;
338 break;
339 }
340 }
341 }
duke6e45e102007-12-01 00:00:00 +0000342}