author | morris |
Fri, 11 Oct 2013 12:40:14 -0700 | |
changeset 21219 | 0c61684d9606 |
parent 10065 | f0dd422f49fa |
child 21637 | fcaf599f308f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
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 |
|
21219
0c61684d9606
7195597: ThreadStateTest gets different results with -Xcomp
morris
parents:
10065
diff
changeset
|
33 |
* @run main/othervm -Xmixed ThreadStateTest |
2 | 34 |
*/ |
35 |
||
36 |
import java.util.concurrent.locks.LockSupport; |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
37 |
import java.util.concurrent.Phaser; |
2 | 38 |
|
39 |
public class ThreadStateTest { |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
40 |
// maximum number of retries when checking for thread state. |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
41 |
static final int MAX_RETRY = 500; |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
42 |
|
2 | 43 |
private static boolean testFailed = false; |
44 |
||
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
45 |
// used to achieve waiting states |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
46 |
static final Object globalLock = new Object(); |
2 | 47 |
|
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 |
} |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
100 |
|
2 | 101 |
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) { |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
107 |
// wait for the thread to transition to the expected state. |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
108 |
// There is a small window between the thread checking the state |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
109 |
// and the thread actual entering that state. |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
110 |
Thread.State state; |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
111 |
int retryCount=0; |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
112 |
while ((state = t.getState()) != expected && retryCount < MAX_RETRY) { |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
113 |
if (state != Thread.State.RUNNABLE) { |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
114 |
throw new RuntimeException("Thread not in expected state yet," + |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
115 |
" but it should at least be RUNNABLE"); |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
116 |
} |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
117 |
goSleep(10); |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
118 |
retryCount++; |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
119 |
} |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
120 |
|
2 | 121 |
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 |
||
133 |
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 { |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
144 |
// Phaser to sync between the main thread putting |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
145 |
// this thread into various states |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
146 |
private Phaser phaser = new Phaser(2); |
2 | 147 |
|
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; |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
160 |
|
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
161 |
private volatile int state = RUNNABLE; |
2 | 162 |
|
163 |
private boolean done = false; |
|
164 |
public void run() { |
|
165 |
// Signal main thread to continue. |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
166 |
phaser.arriveAndAwaitAdvance(); |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
167 |
|
2 | 168 |
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. |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
181 |
phaser.arrive(); |
2 | 182 |
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. |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
192 |
phaser.arrive(); |
2 | 193 |
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. |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
205 |
phaser.arrive(); |
2 | 206 |
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. |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
217 |
phaser.arrive(); |
2 | 218 |
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. |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
226 |
phaser.arrive(); |
2 | 227 |
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. |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
237 |
phaser.arrive(); |
2 | 238 |
System.out.println(" myThread is going to sleep."); |
239 |
try { |
|
240 |
Thread.sleep(1000000); |
|
241 |
} catch (InterruptedException e) { |
|
242 |
// finish sleeping |
|
243 |
} |
|
244 |
break; |
|
245 |
} |
|
246 |
case TERMINATE: { |
|
247 |
done = true; |
|
248 |
// signal main thread. |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
249 |
phaser.arrive(); |
2 | 250 |
break; |
251 |
} |
|
252 |
default: |
|
253 |
break; |
|
254 |
} |
|
255 |
} |
|
256 |
} |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
257 |
|
2 | 258 |
public void waitUntilStarted() { |
259 |
// wait for MyThread. |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
260 |
phaser.arriveAndAwaitAdvance(); |
2 | 261 |
} |
262 |
||
263 |
public void goBlocked() { |
|
264 |
System.out.println("Waiting myThread to go blocked."); |
|
265 |
setState(BLOCKED); |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
266 |
// wait for MyThread to get to a point just before being blocked |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
267 |
phaser.arriveAndAwaitAdvance(); |
2 | 268 |
} |
269 |
||
270 |
public void goWaiting() { |
|
271 |
System.out.println("Waiting myThread to go waiting."); |
|
272 |
setState(WAITING); |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
273 |
// wait for MyThread to get to just before wait on object. |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
274 |
phaser.arriveAndAwaitAdvance(); |
2 | 275 |
} |
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
276 |
|
2 | 277 |
public void goTimedWaiting() { |
278 |
System.out.println("Waiting myThread to go timed waiting."); |
|
279 |
setState(TIMED_WAITING); |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
280 |
// wait for MyThread to get to just before timed wait call. |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
281 |
phaser.arriveAndAwaitAdvance(); |
2 | 282 |
} |
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
283 |
|
2 | 284 |
public void goParked() { |
285 |
System.out.println("Waiting myThread to go parked."); |
|
286 |
setState(PARKED); |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
287 |
// wait for MyThread to get to just before parked. |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
288 |
phaser.arriveAndAwaitAdvance(); |
2 | 289 |
} |
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
290 |
|
2 | 291 |
public void goTimedParked() { |
292 |
System.out.println("Waiting myThread to go timed parked."); |
|
293 |
setState(TIMED_PARKED); |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
294 |
// wait for MyThread to get to just before timed park. |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
295 |
phaser.arriveAndAwaitAdvance(); |
2 | 296 |
} |
297 |
||
298 |
public void goSleeping() { |
|
299 |
System.out.println("Waiting myThread to go sleeping."); |
|
300 |
setState(SLEEPING); |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
301 |
// wait for MyThread to get to just before sleeping |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
302 |
phaser.arriveAndAwaitAdvance(); |
2 | 303 |
} |
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
304 |
|
2 | 305 |
public void terminate() { |
306 |
System.out.println("Waiting myThread to terminate."); |
|
307 |
setState(TERMINATE); |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
308 |
// wait for MyThread to get to just before terminate |
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
309 |
phaser.arriveAndAwaitAdvance(); |
2 | 310 |
} |
311 |
||
312 |
private void setState(int newState) { |
|
313 |
switch (state) { |
|
314 |
case BLOCKED: |
|
315 |
while (state == BLOCKED) { |
|
10065
f0dd422f49fa
7021010: java/lang/Thread/ThreadStateTest.java fails intermittently
chegar
parents:
5506
diff
changeset
|
316 |
goSleep(10); |
2 | 317 |
} |
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 |
} |
|
342 |
} |