/*
* 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 4967283 5080203
* @summary Basic unit test of thread states returned by
* ThreadMXBean.getThreadInfo.getThreadState().
* It also tests lock information returned by ThreadInfo.
*
* @author Mandy Chung
*
* @build ThreadExecutionSynchronizer Utils
* @run main ThreadStateTest
*/
public class ThreadStateTest {
static class Lock {
}
return name;
}
}
// Force thread state initialization now before the test
// verification begins.
// before myThread starts
// Utils.checkThreadState(myThread, Thread.State.NEW);
synchronized (globalLock) {
}
globalLock, null);
globalLock, null);
/*
*********** parkUntil seems not working
* ignore this park case for now.
Bug ID : 5062095
***********************************************
myThread.goParked();
Utils.checkThreadState(myThread, Thread.State.WAITING);
checkLockInfo(myThread, Thread.State.WAITING, null, null);
myThread.goTimedParked();
Utils.checkThreadState(myThread, Thread.State.TIMED_WAITING);
checkLockInfo(myThread, Thread.State.TIMED_WAITING, null, null);
*/
// Utils.checkThreadState(myThread, ThreadState.TERMINATED);
try {
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
throw new RuntimeException(t.getName() +
" expected to have ThreadInfo " +
" but got null.");
}
}
if (!info.isSuspended()) {
}
}
}
throw new RuntimeException(t.getName() +
" expected to have ThreadInfo " +
" but got null.");
}
}
throw new RuntimeException(t.getName() +
" expected not to be blocked on any lock" +
}
throw new RuntimeException(t.getName() +
" expected to be blocked on lock [" + expectedLockName +
"] but got null.");
}
throw new RuntimeException(t.getName() +
" expected to be blocked on lock [" + expectedLockName +
}
throw new RuntimeException("Lock owner is expected " +
}
throw new RuntimeException("Lock owner is expected to be " +
" but got null.");
}
throw new RuntimeException("Lock owner is expected to be " +
}
throw new RuntimeException("Lock owner is expected " +
}
throw new RuntimeException("Lock owner is expected to be " +
}
throw new RuntimeException("Lock owner is expected to be " +
}
if (info.isSuspended()) {
throw new RuntimeException(t.getName() +
}
}
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
break;
}
case TIMED_PARKED: {
// signal main thread.
// give a chance for the main thread to block
break;
}
case SLEEPING: {
// signal main thread.
try {
} catch (InterruptedException e) {
// finish sleeping
interrupted();
}
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 blocked
}
public void goWaiting() {
// wait for MyThread to wait on object.
}
public void goTimedWaiting() {
// wait for MyThread timed wait call.
}
public void goParked() {
// wait for MyThread state change to PARKED.
}
public void goTimedParked() {
// wait for MyThread.
}
public void goSleeping() {
// wait for MyThread.
}
public void terminate() {
// wait for MyThread.
}
switch (state) {
case BLOCKED:
}
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;
}
}
}
}