2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2013-12-26 20:04:16 +00:00
|
|
|
* Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* 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.
|
|
|
|
*
|
2010-05-25 22:58:33 +00:00
|
|
|
* 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.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-06 01:33:26 +00:00
|
|
|
import static java.lang.Thread.State.*;
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
/*
|
|
|
|
* @test
|
2013-11-06 01:33:26 +00:00
|
|
|
* @bug 5014783 8022208
|
2007-12-01 00:00:00 +00:00
|
|
|
* @summary Basic unit test of thread states returned by
|
|
|
|
* Thread.getState().
|
|
|
|
*
|
|
|
|
* @author Mandy Chung
|
2017-05-24 22:24:40 +00:00
|
|
|
* @library /test/lib
|
2017-06-12 19:40:43 +00:00
|
|
|
* @build jdk.test.lib.LockFreeLogger
|
2013-11-06 01:33:26 +00:00
|
|
|
* @build ThreadStateTest ThreadStateController
|
2013-10-11 19:40:14 +00:00
|
|
|
* @run main/othervm -Xmixed ThreadStateTest
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class ThreadStateTest {
|
|
|
|
private static boolean testFailed = false;
|
|
|
|
|
2011-06-23 12:15:14 +00:00
|
|
|
// used to achieve waiting states
|
2013-11-06 01:33:26 +00:00
|
|
|
private static final Object globalLock = new Object();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2013-11-06 01:33:26 +00:00
|
|
|
public static void main(String[] argv) throws Exception {
|
2007-12-01 00:00:00 +00:00
|
|
|
// Call Thread.getState to force all initialization done
|
|
|
|
// before test verification begins.
|
|
|
|
Thread.currentThread().getState();
|
2013-11-06 01:33:26 +00:00
|
|
|
ThreadStateController thread = new ThreadStateController("StateChanger", globalLock);
|
|
|
|
thread.setDaemon(true);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// before myThread starts
|
2013-11-06 01:33:26 +00:00
|
|
|
thread.checkThreadState(NEW);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2013-11-06 01:33:26 +00:00
|
|
|
thread.start();
|
|
|
|
thread.transitionTo(RUNNABLE);
|
|
|
|
thread.checkThreadState(RUNNABLE);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
synchronized (globalLock) {
|
2013-11-06 01:33:26 +00:00
|
|
|
thread.transitionTo(BLOCKED);
|
|
|
|
thread.checkThreadState(BLOCKED);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 01:33:26 +00:00
|
|
|
thread.transitionTo(WAITING);
|
|
|
|
thread.checkThreadState(WAITING);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2013-11-06 01:33:26 +00:00
|
|
|
thread.transitionTo(TIMED_WAITING);
|
|
|
|
thread.checkThreadState(TIMED_WAITING);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2013-11-06 01:33:26 +00:00
|
|
|
thread.transitionToPark(true /* timed park*/);
|
|
|
|
thread.checkThreadState(TIMED_WAITING);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2013-11-06 01:33:26 +00:00
|
|
|
thread.transitionToPark(false /* indefinite park */);
|
|
|
|
thread.checkThreadState(WAITING);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2013-11-06 01:33:26 +00:00
|
|
|
thread.transitionToSleep();
|
|
|
|
thread.checkThreadState(TIMED_WAITING);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2013-11-06 01:33:26 +00:00
|
|
|
thread.transitionTo(TERMINATED);
|
|
|
|
thread.checkThreadState(TERMINATED);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
try {
|
2013-11-06 01:33:26 +00:00
|
|
|
thread.join();
|
2007-12-01 00:00:00 +00:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.out.println("Unexpected exception.");
|
|
|
|
testFailed = true;
|
|
|
|
}
|
2011-06-23 12:15:14 +00:00
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
if (testFailed)
|
|
|
|
throw new RuntimeException("TEST FAILED.");
|
|
|
|
System.out.println("Test passed.");
|
|
|
|
}
|
|
|
|
}
|