This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 56b4d41fd6 GROOVY-11478: Enable GroovyClassLoader to be 
ParallelCapable (backport to 3_0_X)
56b4d41fd6 is described below

commit 56b4d41fd611891afbd8210c21991ea4e9812330
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Oct 3 16:33:34 2024 +1000

    GROOVY-11478: Enable GroovyClassLoader to be ParallelCapable (backport to 
3_0_X)
---
 src/main/java/groovy/lang/GroovyClassLoader.java | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/main/java/groovy/lang/GroovyClassLoader.java 
b/src/main/java/groovy/lang/GroovyClassLoader.java
index ccdb56214a..7e27d7d3e8 100644
--- a/src/main/java/groovy/lang/GroovyClassLoader.java
+++ b/src/main/java/groovy/lang/GroovyClassLoader.java
@@ -78,6 +78,7 @@ import java.util.ArrayList;
 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,
@@ -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 {
@@ -272,8 +276,7 @@ public class GroovyClassLoader extends URLClassLoader {
     }
 
     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 {

Reply via email to