Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/thread/thread007.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/thread007.28* VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]29* VM testbase readme:30* DESCRIPTION31* Try to start the given number of threads starting simultaneously32* when notifyall() is signaled at the stopLine object.33*34* @run main/othervm nsk.stress.thread.thread007 500 2m 5s35*/3637package nsk.stress.thread;3839import java.io.PrintStream;4041/**42* Try to start the given number of threads starting simultaneously43* when <code>notifyall()</code> is signaled at the <code>stopLine</code>44* object.45*/46public class thread007 extends Thread {47/**48* Enable/disable printing of debugging info.49*/50private static boolean DEBUG_MODE = false;5152/**53* The minimal number of threads that the tested JVM must support.54* (This number should be specified by the command-line parameter.55*/56private static int THREADS_EXPECTED = 1000;5758/**59* Timeout (in milliseconds) after which all threads must halt.60*/61private static long TIMEOUT = 300000; // 5 minutes6263/**64* Wait few seconds to allow child threads actually start.65*/66private static long YIELD_TIME = 5000; // 5 seconds6768/**69* Once <code>arg</code> is ``XXXs'', or ``XXXm'', or ``XXXms'',70* return the given number of seconds, minutes, or milliseconds71* correspondingly.72*/73private static long parseTime(String arg) {74for (int i = arg.lastIndexOf("ms"); i > -1; )75return Long.parseLong(arg.substring(0, i));76for (int i = arg.lastIndexOf("s"); i > -1; )77return Long.parseLong(arg.substring(0, i)) * 1000;78for (int i = arg.lastIndexOf("m"); i > -1; )79return Long.parseLong(arg.substring(0, i)) * 60000;80throw new IllegalArgumentException(81"cannot recognize time scale: " + arg);82}8384/**85* Re-invoke to <code>run(args,out)</code> in a JCK style.86*/87public static void main(String args[]) {88int exitCode = run(args, System.out);89System.exit(exitCode + 95);90}9192/**93* Entry point for the JavaTest harness: <code>args[0]</code> must94* prescribe the value for the <code>THREADS_EXPECTED</code> field.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,");114out.println("wait " + YIELD_TIME + " milliseconds to let them go,");115out.println("and halt after " + TIMEOUT + " milliseconds:");116}117118Thread thread[] = new Thread[THREADS_EXPECTED];119int i;120for (i = 0; i < THREADS_EXPECTED; i++)121try {122thread[i] = new thread007();123thread[i].start();124if (DEBUG_MODE)125out.println("Threads started: " + (i + 1));126} catch (OutOfMemoryError oome) {127oome.printStackTrace(out);128out.println("#");129out.println("# The test have FAILED:");130out.println("# Only " + i + " threads could start,");131out.println("# while at least " + THREADS_EXPECTED +132" were expected.");133out.println("#");134return 2;135}136137// Actually start:138synchronized (stopLine) {139stopLine.notifyAll();140}141// ...and let them go:142try {143doSleep(YIELD_TIME);144} catch (InterruptedException ie) {145ie.printStackTrace(out);146out.println("#");147out.println("# OOPS! Could not let threads actually start!");148out.println("#");149return 2;150}151152if (oopsCounter > 0) {153out.println("#");154out.println("# The test have FAILED, because:");155out.println("# " + oopsCounter + " threads were interrupted.");156out.println("#");157return 2;158}159160if (DEBUG_MODE)161out.println("The test have PASSED.");162return 0;163}164165static Object stopLine = new Object();166static int oopsCounter = 0;167168/**169* The thread activity: do nothing special, but do not170* free CPU time so that the thread's memory could not171* be moved to swap file.172*/173public void run() {174synchronized (stopLine) {175try {176stopLine.wait();177} catch (InterruptedException oops) {178oopsCounter++;179return;180}181}182while (!timeout())183continue;184}185186private static long startTime = System.currentTimeMillis();187188/**189* Check if timeout for this test is exceeded.190*/191private boolean timeout() {192long elapsedTime = System.currentTimeMillis() - startTime;193return elapsedTime > TIMEOUT;194}195196/**197* Yield to other threads for the given amount of198* <code>time</code> (milliseconds).199*/200private static void doSleep(long time) throws InterruptedException {201//202// Since Java 2, the method Thread.sleep() doesn't guarantee203// to yield to other threads. So, call Object.wait() to yield:204//205Object lock = new Object(); // local scope, nobody can notify it206synchronized (lock) {207lock.wait(time);208}209}210}211212213