Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/compiler/jsr292/ConcurrentClassLoadingTest.java
41149 views
1
/*
2
* Copyright (c) 2013, 2020, 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 randomness
27
* @bug 8022595
28
* @summary JSR292: deadlock during class loading of MethodHandles, MethodHandleImpl & MethodHandleNatives
29
* @library /test/lib /
30
* @modules java.base/jdk.internal.misc
31
* java.management
32
*
33
* @run main/othervm compiler.jsr292.ConcurrentClassLoadingTest
34
*/
35
36
package compiler.jsr292;
37
38
import jdk.test.lib.Utils;
39
40
import java.util.ArrayList;
41
import java.util.Arrays;
42
import java.util.List;
43
import java.util.Random;
44
import java.util.concurrent.BrokenBarrierException;
45
import java.util.concurrent.CyclicBarrier;
46
47
public class ConcurrentClassLoadingTest {
48
int numThreads = 0;
49
CyclicBarrier l;
50
private static final Random rand = Utils.getRandomInstance();
51
52
public static void main(String[] args) throws Throwable {
53
ConcurrentClassLoadingTest test = new ConcurrentClassLoadingTest();
54
test.parseArgs(args);
55
test.run();
56
}
57
58
void parseArgs(String[] args) {
59
int i = 0;
60
while (i < args.length) {
61
String flag = args[i];
62
switch(flag) {
63
case "-numThreads":
64
numThreads = Integer.parseInt(args[++i]);
65
break;
66
default:
67
throw new Error("Unknown flag: " + flag);
68
}
69
++i;
70
}
71
}
72
73
void init() {
74
if (numThreads == 0) {
75
numThreads = Runtime.getRuntime().availableProcessors();
76
}
77
78
l = new CyclicBarrier(numThreads + 1);
79
80
System.out.printf("Threads: %d\n", numThreads);
81
}
82
83
final List<Loader> loaders = new ArrayList<>();
84
85
void prepare() {
86
List<String> c = new ArrayList<>(Arrays.asList(classNames));
87
88
// Split classes between loading threads
89
int count = (classNames.length / numThreads) + 1;
90
for (int t = 0; t < numThreads; t++) {
91
List<String> sel = new ArrayList<>();
92
93
System.out.printf("Thread #%d:\n", t);
94
for (int i = 0; i < count; i++) {
95
if (c.isEmpty()) {
96
break;
97
}
98
99
int k = rand.nextInt(c.size());
100
String elem = c.remove(k);
101
sel.add(elem);
102
System.out.printf("\t%s\n", elem);
103
}
104
loaders.add(new Loader(sel));
105
}
106
107
// Print diagnostic info when the test hangs
108
Runtime.getRuntime().addShutdownHook(new Thread() {
109
public void run() {
110
boolean alive = false;
111
for (Loader l : loaders) {
112
if (!l.isAlive()) continue;
113
114
if (!alive) {
115
System.out.println("Some threads are still alive:");
116
alive = true;
117
}
118
119
System.out.println(l.getName());
120
for (StackTraceElement elem : l.getStackTrace()) {
121
System.out.println("\t"+elem.toString());
122
}
123
}
124
}
125
});
126
}
127
128
public void run() throws Throwable {
129
init();
130
prepare();
131
132
for (Loader loader : loaders) {
133
loader.start();
134
}
135
136
l.await();
137
138
for (Loader loader : loaders) {
139
loader.join();
140
}
141
}
142
143
class Loader extends Thread {
144
List<String> classes;
145
146
public Loader(List<String> classes) {
147
this.classes = classes;
148
setDaemon(true);
149
}
150
151
@Override
152
public void run() {
153
try {
154
l.await();
155
156
for (String name : classes) {
157
Class.forName(name).getName();
158
}
159
} catch (ClassNotFoundException | BrokenBarrierException | InterruptedException e) {
160
throw new Error(e);
161
}
162
}
163
}
164
165
final static String[] classNames = {
166
"java.lang.invoke.CallSite",
167
"java.lang.invoke.ConstantCallSite",
168
"java.lang.invoke.LambdaConversionException",
169
"java.lang.invoke.LambdaMetafactory",
170
"java.lang.invoke.MethodHandle",
171
"java.lang.invoke.MethodHandleInfo",
172
"java.lang.invoke.MethodHandleProxies",
173
"java.lang.invoke.MethodHandles",
174
"java.lang.invoke.MethodType",
175
"java.lang.invoke.MutableCallSite",
176
"java.lang.invoke.SerializedLambda",
177
"java.lang.invoke.SwitchPoint",
178
"java.lang.invoke.VolatileCallSite",
179
"java.lang.invoke.WrongMethodTypeException"
180
};
181
}
182
183