Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/thread/thread001.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/thread001.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 priority32* lower than the main thread.33*34* @library /test/lib35* @run main/othervm nsk.stress.thread.thread001 500 2m 5s36*/3738package nsk.stress.thread;3940import java.io.PrintStream;41import java.util.Vector;4243/**44* Try to start the given number of threads of the priority45* lower than the main thread.46*/47public class thread001 extends Thread {48/**49* Enable/disable printing of debugging info.50*/51private static boolean DEBUG_MODE = false;5253/**54* The minimal number of threads that the tested JVM must support.55* This number may be re-assigned by the command-line parameter.56*/57private static int THREADS_EXPECTED = 1000;5859/**60* Timeout (in milliseconds) after which all threads must halt.61*/62private static long TIMEOUT = 300000; // 5 minutes6364/**65* Wait few seconds to allow child threads actually start.66*/67private static long YIELD_TIME = 5000; // 5 seconds6869/**70* Once <code>arg</code> is ``XXXs'', or ``XXXm'', or ``XXXms'',71* return the given number of seconds, minutes, or milliseconds72* correspondingly.73*/74private static long parseTime(String arg) {75for (int i = arg.lastIndexOf("ms"); i > -1; )76return Long.parseLong(arg.substring(0, i));77for (int i = arg.lastIndexOf("s"); i > -1; )78return Long.parseLong(arg.substring(0, i)) * 1000;79for (int i = arg.lastIndexOf("m"); i > -1; )80return Long.parseLong(arg.substring(0, i)) * 60000;81throw new IllegalArgumentException(82"cannot recognize time scale: " + arg);83}8485/**86* Re-invoke to <code>run(args,out)</code> in a JCK style.87*/88public static void main(String args[]) {89int exitCode = run(args, System.out);90System.exit(exitCode + 95);91}9293/**94* Entry point for the JavaTest harness: <code>args[0]</code> must95* prescribe the value for the <code>THREADS_EXPECTED</code> field.96*/97public static int run(String args[], PrintStream out) {98if (args.length > 0)99THREADS_EXPECTED = Integer.parseInt(args[0]);100if (args.length > 1)101TIMEOUT = parseTime(args[1]);102if (args.length > 2)103YIELD_TIME = parseTime(args[2]);104if (args.length > 3)105DEBUG_MODE = args[3].toLowerCase().startsWith("-v");106if (args.length > 4) {107out.println("#");108out.println("# Too namy command-line arguments!");109out.println("#");110return 2;111}112113if (DEBUG_MODE) {114out.println("Start " + THREADS_EXPECTED + " threads of lower priority,");115out.println("wait " + YIELD_TIME + " milliseconds to let them go,");116out.println("and halt after " + TIMEOUT + " milliseconds:");117}118119Vector threadList = new Vector();120for (int i = 1; i <= THREADS_EXPECTED; i++)121try {122Thread thread = new thread001();123if (thread.getPriority() == Thread.MIN_PRIORITY) {124out.println("#");125out.println("# Sorry! -- The test cannot execute because");126out.println("# it cannot create threads with lower priority");127out.println("# than that executint 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.setPriority(Thread.MIN_PRIORITY);135threadList.addElement(thread);136thread.start();137138if (DEBUG_MODE)139out.println("Threads started: " + i);140141} catch (OutOfMemoryError oome) {142oome.printStackTrace(out);143out.println("#");144out.println("# The test have FAILED:");145out.println("# Only " + i + " threads could start,");146out.println("# while at least " + THREADS_EXPECTED +147" were expected.");148out.println("#");149return 2;150}151152// Actually let them go:153try {154doSleep(YIELD_TIME);155} catch (InterruptedException ie) {156ie.printStackTrace(out);157out.println("#");158out.println("# OOPS! Could not let threads actually start!");159out.println("#");160return 2;161}162163if (DEBUG_MODE)164out.println("The test have PASSED.");165return 0;166}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() {174while (!timeout())175continue;176}177178private static long startTime = System.currentTimeMillis();179180/**181* Check if timeout for this test is exceeded.182*/183private boolean timeout() {184long elapsedTime = System.currentTimeMillis() - startTime;185return elapsedTime > TIMEOUT;186}187188/**189* Yield to other threads for the given amount of190* <code>time</code> (milliseconds).191*/192private static void doSleep(long time) throws InterruptedException {193//194// Since Java 2, the method Thread.sleep() doesn't guarantee195// to yield to other threads. So, call Object.wait() to yield:196//197Object lock = new Object(); // local scope, nobody can notify it198synchronized (lock) {199lock.wait(time);200}201}202}203204205