Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/thread/thread002.java
41159 views
1
/*
2
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @key stress
27
*
28
* @summary converted from VM testbase nsk/stress/thread/thread002.
29
* VM testbase keywords: [stress, diehard, slow, nonconcurrent, quick]
30
* VM testbase readme:
31
* DESCRIPTION
32
* Try to start the given number of threads of the same
33
* priority that the main thread.
34
*
35
* @run main/othervm nsk.stress.thread.thread002 500 2m 5s
36
*/
37
38
package nsk.stress.thread;
39
40
import java.io.PrintStream;
41
import java.util.Vector;
42
43
/**
44
* Try to start the given number of threads of the same
45
* priority that the main thread.
46
*/
47
public class thread002 extends Thread {
48
/**
49
* Enable/disable printing of debugging info.
50
*/
51
private static boolean DEBUG_MODE = false;
52
53
/**
54
* The minimal number of threads that the tested JVM must support.
55
* (This number should be specified by the command-line parameter.
56
*/
57
private static int THREADS_EXPECTED = 1000;
58
59
/**
60
* Timeout (in milliseconds) after which all threads must halt.
61
*/
62
private static long TIMEOUT = 300000; // 5 minutes
63
64
/**
65
* Wait few seconds to allow child threads actually start.
66
*/
67
private static long YIELD_TIME = 5000; // 5 seconds
68
69
/**
70
* Once <code>arg</code> is ``XXXs'', or ``XXXm'', or ``XXXms'',
71
* return the given number of seconds, minutes, or milliseconds
72
* correspondingly.
73
*/
74
private static long parseTime(String arg) {
75
for (int i = arg.lastIndexOf("ms"); i > -1; )
76
return Long.parseLong(arg.substring(0, i));
77
for (int i = arg.lastIndexOf("s"); i > -1; )
78
return Long.parseLong(arg.substring(0, i)) * 1000;
79
for (int i = arg.lastIndexOf("m"); i > -1; )
80
return Long.parseLong(arg.substring(0, i)) * 60000;
81
throw new IllegalArgumentException(
82
"cannot recognize time scale: " + arg);
83
}
84
85
/**
86
* Re-invoke to <code>run(args,out)</code> in a JCK style.
87
*/
88
public static void main(String args[]) {
89
System.exit(run(args, System.out) + 95);
90
}
91
92
/**
93
* Entry point for the JavaTest harness: <code>args[0]</code> must
94
* prescribe the value for the <code>THREADS_EXPECTED</code> field.
95
*/
96
public static int run(String args[], PrintStream out) {
97
if (args.length > 0)
98
THREADS_EXPECTED = Integer.parseInt(args[0]);
99
if (args.length > 1)
100
TIMEOUT = parseTime(args[1]);
101
if (args.length > 2)
102
YIELD_TIME = parseTime(args[2]);
103
if (args.length > 3)
104
DEBUG_MODE = args[3].toLowerCase().startsWith("-v");
105
if (args.length > 4) {
106
out.println("#");
107
out.println("# Too namy command-line arguments!");
108
out.println("#");
109
return 2;
110
}
111
112
if (DEBUG_MODE) {
113
out.println("Start " + THREADS_EXPECTED + " threads,");
114
out.println("wait " + YIELD_TIME + " milliseconds to let them go,");
115
out.println("and halt after " + TIMEOUT + " milliseconds:");
116
}
117
118
Vector threadList = new Vector();
119
for (int i = 1; i <= THREADS_EXPECTED; i++)
120
try {
121
Thread thread = new thread002();
122
threadList.addElement(thread);
123
thread.start();
124
125
if (DEBUG_MODE)
126
out.println("Threads started: " + i);
127
128
} catch (OutOfMemoryError oome) {
129
oome.printStackTrace(out);
130
out.println("#");
131
out.println("# The test have FAILED:");
132
out.println("# Only " + i + " threads could start,");
133
out.println("# while at least " + THREADS_EXPECTED +
134
" were expected.");
135
out.println("#");
136
return 2;
137
}
138
139
// Actually let them go:
140
try {
141
doSleep(YIELD_TIME);
142
} catch (InterruptedException ie) {
143
ie.printStackTrace(out);
144
out.println("#");
145
out.println("# OOPS! Could not let threads actually start!");
146
out.println("#");
147
return 2;
148
}
149
150
if (DEBUG_MODE)
151
out.println("The test have PASSED.");
152
return 0;
153
}
154
155
/**
156
* The thread activity: do nothing special, but do not
157
* free CPU time so that the thread's memory could not
158
* be moved to swap file.
159
*/
160
public void run() {
161
while (!timeout())
162
continue;
163
}
164
165
private static long startTime = System.currentTimeMillis();
166
167
/**
168
* Check if timeout for this test is exceeded.
169
*/
170
private boolean timeout() {
171
long elapsedTime = System.currentTimeMillis() - startTime;
172
return elapsedTime > TIMEOUT;
173
}
174
175
/**
176
* Yield to other threads for the given amount of
177
* <code>time</code> (milliseconds).
178
*/
179
private static void doSleep(long time) throws InterruptedException {
180
//
181
// Since Java 2, the method Thread.sleep() doesn't guarantee
182
// to yield to other threads. So, call Object.wait() to yield:
183
//
184
Object lock = new Object(); // local scope, nobody can notify it
185
synchronized (lock) {
186
lock.wait(time);
187
}
188
}
189
}
190
191