Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/thread/thread008.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/thread008.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 lower priority32* starting simultaneously when notifyall() is signaled at the33* stopLine object.34*35* @run main/othervm nsk.stress.thread.thread008 500 2m 5s36*/3738package nsk.stress.thread;3940import java.io.PrintStream;4142/**43* Try to start the given number of threads of lower priority and44* starting simultaneously when <code>notifyall()</code> is signaled45* at the <code>stopLine</code> object.46*/47public class thread008 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 should be specified 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}118119Thread thread[] = new Thread[THREADS_EXPECTED];120int i;121for (i = 0; i < THREADS_EXPECTED; i++)122try {123thread[i] = new thread008();124if (thread[i].getPriority() == Thread.MIN_PRIORITY) {125out.println("#");126out.println("# Sorry! -- The test cannot execute because");127out.println("# it cannot create threads with lower priority");128out.println("# than that executint run(args[],out) method.");129out.println("#");130out.println("# However, since no JVM mistakes were found,");131out.println("# the test finishes as PASSED.");132out.println("#");133return 0;134}135thread[i].setPriority(Thread.MIN_PRIORITY);136thread[i].start();137if (DEBUG_MODE)138out.println("Threads started: " + (i + 1));139} catch (OutOfMemoryError oome) {140oome.printStackTrace(out);141out.println("#");142out.println("# The test have FAILED:");143out.println("# Only " + i + " threads could start,");144out.println("# while at least " + THREADS_EXPECTED +145" were expected.");146out.println("#");147return 2;148}149150// Actually start:151synchronized (stopLine) {152stopLine.notifyAll();153}154// ...and let them go:155try {156doSleep(YIELD_TIME);157} catch (InterruptedException ie) {158ie.printStackTrace(out);159out.println("#");160out.println("# OOPS! Could not let threads actually start!");161out.println("#");162return 2;163}164165if (oopsCounter > 0) {166out.println("#");167out.println("# The test have FAILED, because:");168out.println("# " + oopsCounter + " threads were interrupted.");169out.println("#");170return 2;171}172173if (DEBUG_MODE)174out.println("The test have PASSED.");175return 0;176}177178static Object stopLine = new Object();179static int oopsCounter = 0;180181/**182* The thread activity: do nothing special, but do not183* free CPU time so that the thread's memory could not184* be moved to swap file.185*/186public void run() {187synchronized (stopLine) {188try {189stopLine.wait();190} catch (InterruptedException oops) {191oopsCounter++;192return;193}194}195while (!timeout())196continue;197}198199private static long startTime = System.currentTimeMillis();200201/**202* Check if timeout for this test is exceeded.203*/204private boolean timeout() {205long elapsedTime = System.currentTimeMillis() - startTime;206return elapsedTime > TIMEOUT;207}208209/**210* Yield to other threads for the given amount of211* <code>time</code> (milliseconds).212*/213private static void doSleep(long time) throws InterruptedException {214//215// Since Java 2, the method Thread.sleep() doesn't guarantee216// to yield to other threads. So, call Object.wait() to yield:217//218Object lock = new Object(); // local scope, nobody can notify it219synchronized (lock) {220lock.wait(time);221}222}223}224225226