Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jpda/StateTestThread.java
41161 views
/*1* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package nsk.share.jpda;2324import nsk.share.TestBug;25import nsk.share.locks.MonitorLockingThread;2627/*28* StateTestThread sequentially switches its state in following order:29* - thread not started30* - thread is running31* - thread is sleeping32* - thread in Object.wait()33* - thread wait on java monitor34* - thread is finished35*36* To use this class create new instance of StateTestThread and sequentially call method nextState().37*/38public class StateTestThread extends Thread {39// thread states available through ThreadReference.state()40public static String stateTestThreadStates[] = { "UNKNOWN", "RUNNING", "SLEEPING", "WAIT", "MONITOR", "ZOMBIE" };4142private Object waitOnObject = new Object();4344public StateTestThread(String name) {45super(name);46}4748private volatile boolean isRunning;4950private volatile boolean waitState;5152public int getCurrentState() {53return currentState;54}5556private MonitorLockingThread auxiliaryThread = new MonitorLockingThread(this);5758private boolean isExecutedWithErrors;5960private volatile boolean readyToBeBlocked;6162private String errorMessage;6364public void run() {65isRunning = true;6667// running state68while (isRunning)69;7071try {72// sleeping state73sleep(Long.MAX_VALUE);74} catch (InterruptedException e) {75// expected exception76}7778synchronized (waitOnObject) {79try {80// wait state81while (waitState)82waitOnObject.wait();83} catch (InterruptedException e) {84isExecutedWithErrors = true;85errorMessage = "StateTestThread was unexpected interrupted during waiting";86}87}8889// start auxiliary thread which should acquire 'this' lock90auxiliaryThread.acquireLock();9192readyToBeBlocked = true;9394// try acquire the same lock as auxiliaryThread, switch state to 'wait on monitor'95synchronized (this) {9697}98}99100private int currentState = 1;101102public void nextState() {103// check is thread states change as expected104if (isExecutedWithErrors)105throw new TestBug(errorMessage);106107switch (currentState++) {108case 1:109// start thread110start();111112while (!isRunning)113Thread.yield();114115break;116case 2:117// stop running118isRunning = false;119120while (this.getState() != Thread.State.TIMED_WAITING)121Thread.yield();122123break;124case 3:125waitState = true;126127// stop sleeping128interrupt();129130while (getState() != Thread.State.WAITING)131Thread.yield();132133break;134case 4:135waitState = false;136137// stop wait138synchronized (waitOnObject) {139waitOnObject.notify();140}141142while (!readyToBeBlocked || (getState() != Thread.State.BLOCKED))143Thread.yield();144145break;146case 5:147// let StateTestThread thread acquire lock148auxiliaryThread.releaseLock();149try {150join();151} catch (InterruptedException e) {152throw new TestBug("Unexpected exception: " + e);153}154break;155156default:157throw new TestBug("Invalid thread state");158}159}160}161162163