25
25
import java .io .*;
26
26
import java .net .*;
27
27
import java .lang .reflect .Field ;
28
+ import java .util .concurrent .atomic .AtomicInteger ;
28
29
29
30
30
31
// This test helper is parameterized by:
@@ -78,7 +79,7 @@ public static void run(String args[], ClassLoader loaders[]) throws Throwable {
78
79
if ("FINGERPRINT_MODE" .equals (args [1 ])) {
79
80
mode = FINGERPRINT_MODE ;
80
81
classLoaders = new ClassLoader [NUM_THREADS ];
81
- for (int i = 0 ; i < NUM_THREADS ; i ++) {
82
+ for (int i = 0 ; i < NUM_THREADS ; i ++) {
82
83
URL url = new File (customJar ).toURI ().toURL ();
83
84
URL [] urls = new URL [] {url };
84
85
classLoaders [i ] = new URLClassLoader (urls );
@@ -93,7 +94,7 @@ public static void run(String args[], ClassLoader loaders[]) throws Throwable {
93
94
System .out .println ("Start Parallel Load ..." );
94
95
95
96
Thread thread [] = new Thread [NUM_THREADS ];
96
- for (int i = 0 ; i < NUM_THREADS ; i ++) {
97
+ for (int i = 0 ; i < NUM_THREADS ; i ++) {
97
98
Thread t = new ParallelLoadThread (i );
98
99
t .start ();
99
100
thread [i ] = t ;
@@ -103,7 +104,7 @@ public static void run(String args[], ClassLoader loaders[]) throws Throwable {
103
104
watchdog .setDaemon (true );
104
105
watchdog .start ();
105
106
106
- for (int i = 0 ; i < NUM_THREADS ; i ++) {
107
+ for (int i = 0 ; i < NUM_THREADS ; i ++) {
107
108
thread [i ].join ();
108
109
}
109
110
System .out .println ("Parallel Load ... done" );
@@ -128,8 +129,13 @@ public void run() {
128
129
129
130
130
131
class ParallelLoadThread extends Thread {
131
- static int num_ready [] = new int [ParallelLoad .MAX_CLASSES ];
132
- static Object lock = new Object ();
132
+ static AtomicInteger num_ready [];
133
+ static {
134
+ num_ready = new AtomicInteger [ParallelLoad .MAX_CLASSES ];
135
+ for (int i = 0 ; i < ParallelLoad .MAX_CLASSES ; i ++) {
136
+ num_ready [i ] = new AtomicInteger ();
137
+ }
138
+ }
133
139
static String transformMode =
134
140
System .getProperty ("appcds.parallel.transform.mode" , "none" );
135
141
@@ -153,35 +159,36 @@ private static void log(String msg, Object... args) {
153
159
}
154
160
155
161
private void run0 () throws Throwable {
156
- for (int i =0 ; i <ParallelLoad .MAX_CLASSES ; i ++) {
157
- synchronized (lock ) {
158
- num_ready [i ] ++;
159
- while (num_ready [i ] < ParallelLoad .NUM_THREADS ) {
160
- lock .wait ();
161
- }
162
- lock .notifyAll ();
163
- }
164
- log ("this = %s %d" , this , i );
162
+ for (int i = 0 ; i < ParallelLoad .MAX_CLASSES ; i ++) {
165
163
String className = "ParallelClass" + i ;
166
- if (transformMode .equals ("cflh" ))
164
+ if (transformMode .equals ("cflh" )) {
167
165
className = "ParallelClassTr" + i ;
168
-
166
+ }
169
167
Class clazz = null ;
170
168
171
- switch (ParallelLoad .loaderType ) {
172
- case ParallelLoad .SYSTEM_LOADER :
173
- clazz = Class .forName (className );
174
- break ;
175
- case ParallelLoad .SINGLE_CUSTOM_LOADER :
176
- clazz = ParallelLoad .classLoaders [0 ].loadClass (className );
177
- break ;
178
- case ParallelLoad .MULTI_CUSTOM_LOADER :
179
- clazz = ParallelLoad .classLoaders [thread_id ].loadClass (className );
180
- break ;
169
+ // Spin until every thread is ready to proceed
170
+ num_ready [i ].incrementAndGet ();
171
+ while (num_ready [i ].intValue () < ParallelLoad .NUM_THREADS ) {
172
+ ;
173
+ }
174
+
175
+ { // Avoid logging in this block so the threads can proceed without
176
+ // waiting for the stdout lock, etc.
177
+ switch (ParallelLoad .loaderType ) {
178
+ case ParallelLoad .SYSTEM_LOADER :
179
+ clazz = Class .forName (className );
180
+ break ;
181
+ case ParallelLoad .SINGLE_CUSTOM_LOADER :
182
+ clazz = ParallelLoad .classLoaders [0 ].loadClass (className );
183
+ break ;
184
+ case ParallelLoad .MULTI_CUSTOM_LOADER :
185
+ clazz = ParallelLoad .classLoaders [thread_id ].loadClass (className );
186
+ break ;
187
+ }
188
+ testTransformation (clazz );
181
189
}
182
190
183
- log ("clazz = %s" , clazz );
184
- testTransformation (clazz );
191
+ log ("thread[%d] t = %s, c = %s, l = %s" , thread_id , this , clazz , clazz .getClassLoader ());
185
192
}
186
193
}
187
194
0 commit comments