Skip to content

Commit

Permalink
GROOVY-11478: Enable GroovyClassLoader to be ParallelCapable (backpor…
Browse files Browse the repository at this point in the history
…t to 3_0_X)
  • Loading branch information
paulk-asert committed Oct 3, 2024
1 parent 0b9d319 commit 56b4d41
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main/java/groovy/lang/GroovyClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
* A ClassLoader which can load Groovy classes. The loaded classes are cached,
Expand Down Expand Up @@ -105,8 +106,11 @@ public class GroovyClassLoader extends URLClassLoader {
private final CompilerConfiguration config;
private String sourceEncoding;
private Boolean recompile;
// use 1000000 as offset to avoid conflicts with names from the GroovyShell
private static int scriptNameCounter = 1000000;
private static final AtomicInteger scriptNameCounter = new AtomicInteger(1_000_000); // 1,000,000 avoids conflicts with names from the GroovyShell

static {
registerAsParallelCapable();
}

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

public synchronized String generateScriptName() {
scriptNameCounter++;
return "script" + scriptNameCounter + ".groovy";
return "script" + scriptNameCounter.getAndIncrement() + ".groovy";
}

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

0 comments on commit 56b4d41

Please sign in to comment.