Skip to content

Commit 56b4d41

Browse files
committed
GROOVY-11478: Enable GroovyClassLoader to be ParallelCapable (backport to 3_0_X)
1 parent 0b9d319 commit 56b4d41

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/main/java/groovy/lang/GroovyClassLoader.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import java.util.Collection;
7979
import java.util.Enumeration;
8080
import java.util.Map;
81+
import java.util.concurrent.atomic.AtomicInteger;
8182

8283
/**
8384
* A ClassLoader which can load Groovy classes. The loaded classes are cached,
@@ -105,8 +106,11 @@ public class GroovyClassLoader extends URLClassLoader {
105106
private final CompilerConfiguration config;
106107
private String sourceEncoding;
107108
private Boolean recompile;
108-
// use 1000000 as offset to avoid conflicts with names from the GroovyShell
109-
private static int scriptNameCounter = 1000000;
109+
private static final AtomicInteger scriptNameCounter = new AtomicInteger(1_000_000); // 1,000,000 avoids conflicts with names from the GroovyShell
110+
111+
static {
112+
registerAsParallelCapable();
113+
}
110114

111115
private GroovyResourceLoader resourceLoader = new GroovyResourceLoader() {
112116
public URL loadGroovySource(final String filename) throws MalformedURLException {
@@ -272,8 +276,7 @@ public Class parseClass(String text) throws CompilationFailedException {
272276
}
273277

274278
public synchronized String generateScriptName() {
275-
scriptNameCounter++;
276-
return "script" + scriptNameCounter + ".groovy";
279+
return "script" + scriptNameCounter.getAndIncrement() + ".groovy";
277280
}
278281

279282
public Class parseClass(final Reader reader, final String fileName) throws CompilationFailedException {

0 commit comments

Comments
 (0)