Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/thread/thread006.java
41159 views
/*1* Copyright (c) 2000, 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*/2223/*24* @test25* @key stress26*27* @summary converted from VM testbase nsk/stress/thread/thread006.28* VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]29* VM testbase readme:30* DESCRIPTION31* Try many threads of lower priority32* starting simultaneously.33*34* @run main/othervm nsk.stress.thread.thread006 500 2m 5s35*/3637package nsk.stress.thread;383940import java.io.PrintStream;4142/**43* Try many threads of lower priority starting simultaneously.44*/45public class thread006 extends Thread {46/**47* Enable/disable printing of debugging info.48*/49private static boolean DEBUG_MODE = false;5051/**52* The minimal number of threads that the tested JVM must support.53* (This number should be specified by the command-line parameter.54*/55private static int THREADS_EXPECTED = 1000;5657/**58* Timeout (in milliseconds) after which all threads must halt.59*/60private static long TIMEOUT = 300000; // 5 minutes6162/**63* Wait few seconds to allow child threads actually start.64*/65private static long YIELD_TIME = 5000; // 5 seconds6667/**68* Once <code>arg</code> is ``XXXs'', or ``XXXm'', or ``XXXms'',69* return the given number of seconds, minutes, or milliseconds70* correspondingly.71*/72private static long parseTime(String arg) {73for (int i = arg.lastIndexOf("ms"); i > -1; )74return Long.parseLong(arg.substring(0, i));75for (int i = arg.lastIndexOf("s"); i > -1; )76return Long.parseLong(arg.substring(0, i)) * 1000;77for (int i = arg.lastIndexOf("m"); i > -1; )78return Long.parseLong(arg.substring(0, i)) * 60000;79throw new IllegalArgumentException(80"cannot recognize time scale: " + arg);81}8283/**84* Re-invoke to <code>run(args,out)</code> in a JCK style.85*/86public static void main(String args[]) {87int exitCode = run(args, System.out);88System.exit(exitCode + 95);89}9091/**92* JavaTest-like entry: <code>args[]</code> may reset93* <code>THREADS_EXPECTED</code>, <code>TIMEOUT</code>94* and <code>YIELD_TIME</code> values.95*/96public static int run(String args[], PrintStream out) {97if (args.length > 0)98THREADS_EXPECTED = Integer.parseInt(args[0]);99if (args.length > 1)100TIMEOUT = parseTime(args[1]);101if (args.length > 2)102YIELD_TIME = parseTime(args[2]);103if (args.length > 3)104DEBUG_MODE = args[3].toLowerCase().startsWith("-v");105if (args.length > 4) {106out.println("#");107out.println("# Too namy command-line arguments!");108out.println("#");109return 2;110}111112if (DEBUG_MODE) {113out.println("Start " + THREADS_EXPECTED + " threads of lower priority,");114out.println("wait " + YIELD_TIME + " milliseconds to let them go,");115out.println("and halt after " + TIMEOUT + " milliseconds:");116}117118Thread thread[] = new Thread[THREADS_EXPECTED];119for (int i = 0; i < THREADS_EXPECTED; i++)120try {121thread[i] = new thread006();122thread[i].setPriority(Thread.MIN_PRIORITY);123if (thread[i].getPriority() == currentThread().getPriority()) {124out.println("*");125out.println("* Sorry! -- The test cannot execute because");126out.println("* it cannot create threads with lower priority");127out.println("* than that executing run(args[],out) method.");128out.println("*");129out.println("* However, since no JVM mistakes were found,");130out.println("* the test finishes as PASSED.");131out.println("*");132return 0;133}134thread[i].start();135if (DEBUG_MODE)136out.println("Threads started: " + (i + 1));137} catch (OutOfMemoryError oome) {138oome.printStackTrace(out);139out.println("#");140out.println("# The test have FAILED:");141out.println("# Only " + i + " threads could start,");142out.println("# while at least " + THREADS_EXPECTED +143" were expected.");144out.println("#");145return 2;146}147148// Actually start:149GO = true;150// ...and let them go:151try {152doSleep(YIELD_TIME);153} catch (InterruptedException ie) {154ie.printStackTrace(out);155out.println("#");156out.println("# OOPS! Could not let threads actually start!");157out.println("#");158return 2;159}160STOP = true;161162if (DEBUG_MODE)163out.println("The test have PASSED.");164return 0;165}166167private static boolean GO = false;168private static boolean STOP = false;169170171/**172* The thread activity: do nothing special, but do not173* free CPU time so that the thread's memory could not174* be moved to swap file.175*/176public void run() {177while (!GO && !timeout())178Thread.yield();179while (!STOP && !timeout())180;181}182183private static long startTime = System.currentTimeMillis();184185/**186* Check if timeout for this test is exceeded.187*/188private boolean timeout() {189long elapsedTime = System.currentTimeMillis() - startTime;190return elapsedTime > TIMEOUT;191}192193/**194* Yield to other threads for the given amount of195* <code>time</code> (milliseconds).196*/197private static void doSleep(long time) throws InterruptedException {198//199// Since Java 2, the method Thread.sleep() doesn't guarantee200// to yield to other threads. So, call Object.wait() to yield:201//202Object lock = new Object(); // local scope, nobody can notify it203synchronized (lock) {204lock.wait(time);205}206}207}208209210