Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/thread/thread002.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/thread002.28* VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]29* VM testbase readme:30* DESCRIPTION31* Try to start the given number of threads of the same32* priority that the main thread.33*34* @run main/othervm nsk.stress.thread.thread002 500 2m 5s35*/3637package nsk.stress.thread;3839import java.io.PrintStream;40import java.util.Vector;4142/**43* Try to start the given number of threads of the same44* priority that the main thread.45*/46public class thread002 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[]) {88System.exit(run(args, System.out) + 95);89}9091/**92* Entry point for the JavaTest harness: <code>args[0]</code> must93* prescribe the value for the <code>THREADS_EXPECTED</code> field.94*/95public static int run(String args[], PrintStream out) {96if (args.length > 0)97THREADS_EXPECTED = Integer.parseInt(args[0]);98if (args.length > 1)99TIMEOUT = parseTime(args[1]);100if (args.length > 2)101YIELD_TIME = parseTime(args[2]);102if (args.length > 3)103DEBUG_MODE = args[3].toLowerCase().startsWith("-v");104if (args.length > 4) {105out.println("#");106out.println("# Too namy command-line arguments!");107out.println("#");108return 2;109}110111if (DEBUG_MODE) {112out.println("Start " + THREADS_EXPECTED + " threads,");113out.println("wait " + YIELD_TIME + " milliseconds to let them go,");114out.println("and halt after " + TIMEOUT + " milliseconds:");115}116117Vector threadList = new Vector();118for (int i = 1; i <= THREADS_EXPECTED; i++)119try {120Thread thread = new thread002();121threadList.addElement(thread);122thread.start();123124if (DEBUG_MODE)125out.println("Threads started: " + i);126127} catch (OutOfMemoryError oome) {128oome.printStackTrace(out);129out.println("#");130out.println("# The test have FAILED:");131out.println("# Only " + i + " threads could start,");132out.println("# while at least " + THREADS_EXPECTED +133" were expected.");134out.println("#");135return 2;136}137138// Actually let them go:139try {140doSleep(YIELD_TIME);141} catch (InterruptedException ie) {142ie.printStackTrace(out);143out.println("#");144out.println("# OOPS! Could not let threads actually start!");145out.println("#");146return 2;147}148149if (DEBUG_MODE)150out.println("The test have PASSED.");151return 0;152}153154/**155* The thread activity: do nothing special, but do not156* free CPU time so that the thread's memory could not157* be moved to swap file.158*/159public void run() {160while (!timeout())161continue;162}163164private static long startTime = System.currentTimeMillis();165166/**167* Check if timeout for this test is exceeded.168*/169private boolean timeout() {170long elapsedTime = System.currentTimeMillis() - startTime;171return elapsedTime > TIMEOUT;172}173174/**175* Yield to other threads for the given amount of176* <code>time</code> (milliseconds).177*/178private static void doSleep(long time) throws InterruptedException {179//180// Since Java 2, the method Thread.sleep() doesn't guarantee181// to yield to other threads. So, call Object.wait() to yield:182//183Object lock = new Object(); // local scope, nobody can notify it184synchronized (lock) {185lock.wait(time);186}187}188}189190191