/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 5014783
* @summary Basic unit test of thread states returned by
* Thread.getState().
*
* @author Mandy Chung
*
* @build ThreadStateTest
* @run main ThreadStateTest
*/
public class ThreadStateTest {
// maximum number of retries when checking for thread state.
private static boolean testFailed = false;
// used to achieve waiting states
// Call Thread.getState to force all initialization done
// before test verification begins.
// before myThread starts
synchronized (globalLock) {
}
/*
*********** park and parkUntil seems not working
* ignore this case for now.
* Bug ID 5062095
***********************************************
myThread.goParked();
checkThreadState(myThread, Thread.State.WAITING);
myThread.goTimedParked();
checkThreadState(myThread, Thread.State.TIMED_WAITING);
*/
try {
} catch (InterruptedException e) {
e.printStackTrace();
testFailed = true;
}
if (testFailed)
throw new RuntimeException("TEST FAILED.");
}
// wait for the thread to transition to the expected state.
// There is a small window between the thread checking the state
// and the thread actual entering that state.
int retryCount=0;
throw new RuntimeException("Thread not in expected state yet," +
" but it should at least be RUNNABLE");
}
goSleep(10);
retryCount++;
}
expected + " but got null.");
}
}
}
try {
} catch (InterruptedException e) {
e.printStackTrace();
testFailed = true;
}
}
// Phaser to sync between the main thread putting
// this thread into various states
super(name);
}
private boolean done = false;
public void run() {
// Signal main thread to continue.
while (!done) {
switch (state) {
case RUNNABLE: {
double sum = 0;
for (int i = 0; i < 1000; i++) {
sum += x - r;
}
break;
}
case BLOCKED: {
// signal main thread.
synchronized (globalLock) {
// finish blocking
}
break;
}
case WAITING: {
synchronized (globalLock) {
// signal main thread.
try {
globalLock.wait();
} catch (InterruptedException e) {
// ignore
}
}
break;
}
case TIMED_WAITING: {
synchronized (globalLock) {
// signal main thread.
try {
} catch (InterruptedException e) {
// ignore
}
}
break;
}
case PARKED: {
// signal main thread.
LockSupport.park();
// give a chance for the main thread to block
goSleep(10);
break;
}
case TIMED_PARKED: {
// signal main thread.
// give a chance for the main thread to block
goSleep(10);
break;
}
case SLEEPING: {
// signal main thread.
try {
} catch (InterruptedException e) {
// finish sleeping
}
break;
}
case TERMINATE: {
done = true;
// signal main thread.
break;
}
default:
break;
}
}
}
public void waitUntilStarted() {
// wait for MyThread.
}
public void goBlocked() {
// wait for MyThread to get to a point just before being blocked
}
public void goWaiting() {
// wait for MyThread to get to just before wait on object.
}
public void goTimedWaiting() {
// wait for MyThread to get to just before timed wait call.
}
public void goParked() {
// wait for MyThread to get to just before parked.
}
public void goTimedParked() {
// wait for MyThread to get to just before timed park.
}
public void goSleeping() {
// wait for MyThread to get to just before sleeping
}
public void terminate() {
// wait for MyThread to get to just before terminate
}
switch (state) {
case BLOCKED:
goSleep(10);
}
break;
case WAITING:
case TIMED_WAITING:
synchronized (globalLock) {
globalLock.notify();
}
break;
case PARKED:
case TIMED_PARKED:
LockSupport.unpark(this);
break;
case SLEEPING:
this.interrupt();
break;
default:
break;
}
}
}
}