Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/thread/thread005.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/thread005.28* VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]29* VM testbase readme:30* DESCRIPTION31* Try many threads starting simultaneously.32*33* @run main/othervm nsk.stress.thread.thread005 500 2m 5s34*/3536package nsk.stress.thread;3738import java.io.PrintStream;3940/**41* Try many threads starting simultaneously.42*/43public class thread005 extends Thread {44/**45* Enable/disable printing of debugging info.46*/47private static boolean DEBUG_MODE = false;4849/**50* The number of threads that the tested JVM must support.51*/52private static int THREADS_EXPECTED = 1000;5354/**55* Timeout (in milliseconds) after which all threads must halt.56*/57private static long TIMEOUT = 300000; // 5 minutes5859/**60* Wait few seconds to allow child threads actually start.61*/62private static long YIELD_TIME = 5000; // 5 seconds6364/**65* Once <code>arg</code> is ``XXXs'', or ``XXXm'', or ``XXXms'',66* return the given number of seconds, minutes, or milliseconds67* correspondingly.68*/69private static long parseTime(String arg) {70for (int i = arg.lastIndexOf("ms"); i > -1; )71return Long.parseLong(arg.substring(0, i));72for (int i = arg.lastIndexOf("s"); i > -1; )73return Long.parseLong(arg.substring(0, i)) * 1000;74for (int i = arg.lastIndexOf("m"); i > -1; )75return Long.parseLong(arg.substring(0, i)) * 60000;76throw new IllegalArgumentException(77"cannot recognize time scale: " + arg);78}7980/**81* Re-invoke to <code>run(args,out)</code> to follow JCK style.82*/83public static void main(String args[]) {84int exitCode = run(args, System.out);85System.exit(exitCode + 95);86}8788/**89* JavaTest-like entry: <code>args[]</code> may reset90* <code>THREADS_EXPECTED</code> and <code>TIMEOUT</code>91* values.92*/93public static int run(String args[], PrintStream out) {94if (args.length > 0)95THREADS_EXPECTED = Integer.parseInt(args[0]);96if (args.length > 1)97TIMEOUT = parseTime(args[1]);98if (args.length > 2)99YIELD_TIME = parseTime(args[2]);100if (args.length > 3)101DEBUG_MODE = args[3].toLowerCase().startsWith("-v");102if (args.length > 4) {103out.println("#");104out.println("# Too namy command-line arguments!");105out.println("#");106return 2;107}108109if (DEBUG_MODE) {110out.println(111"Start " + THREADS_EXPECTED + " threads, "112+ "halt after " + TIMEOUT + " milliseconds:");113}114115Thread thread[] = new Thread[THREADS_EXPECTED];116for (int i = 0; i < THREADS_EXPECTED; i++)117try {118thread[i] = new thread005();119thread[i].start();120if (DEBUG_MODE)121out.println("Threads started: " + (i + 1));122} catch (OutOfMemoryError oome) {123oome.printStackTrace(out);124out.println("#");125out.println("# The test have FAILED:");126out.println("# Only " + i + " threads could start,");127out.println("# while at least " + THREADS_EXPECTED +128" were expected.");129out.println("#");130return 2;131}132133// Actually start:134GO = true;135// ...and let them go:136try {137doSleep(YIELD_TIME);138} catch (InterruptedException ie) {139ie.printStackTrace(out);140out.println("#");141out.println("# OOPS! Could not let threads actually start!");142out.println("#");143return 2;144}145STOP = true;146147if (DEBUG_MODE)148out.println("The test have PASSED.");149return 0;150}151152private static boolean GO = false;153private static boolean STOP = false;154155/**156* The thread activity: do nothing special, but do not157* free CPU time so that the thread's memory could not158* be moved to swap file.159*/160public void run() {161while (!GO && !timeout())162Thread.yield();163while (!STOP && !timeout())164;165}166167private static long startTime = System.currentTimeMillis();168169/**170* Check if timeout for this test is exceeded.171*/172private boolean timeout() {173long elapsedTime = System.currentTimeMillis() - startTime;174return elapsedTime > TIMEOUT;175}176177/**178* Yield to other threads for the given amount of179* <code>time</code> (milliseconds).180*/181private static void doSleep(long time) throws InterruptedException {182//183// Since Java 2, the method Thread.sleep() doesn't guarantee184// to yield to other threads. So, call Object.wait() to yield:185//186Object lock = new Object(); // local scope, nobody can notify it187synchronized (lock) {188lock.wait(time);189}190}191}192193194