You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2023/04/15 12:06:32 UTC

[groovy] branch GROOVY_4_0_X updated: GROOVY-11015: [GINQ]Leverage the power of Virtual Thread

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

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


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new c9f4ce4e30 GROOVY-11015: [GINQ]Leverage the power of Virtual Thread
c9f4ce4e30 is described below

commit c9f4ce4e3000a27bd21b7032cd88351384c7035f
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Apr 15 19:19:51 2023 +0800

    GROOVY-11015: [GINQ]Leverage the power of Virtual Thread
    
    (cherry picked from commit 16117a70e3c639829e3914721263c6a0946a98ba)
---
 .../collection/runtime/QueryableHelper.groovy      | 31 +++++++++++++++++-----
 1 file changed, 25 insertions(+), 6 deletions(-)

diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableHelper.groovy b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableHelper.groovy
index aa2376399e..1ffd8e21aa 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableHelper.groovy
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableHelper.groovy
@@ -18,6 +18,7 @@
  */
 package org.apache.groovy.ginq.provider.collection.runtime
 
+import groovy.transform.CompileDynamic
 import groovy.transform.CompileStatic
 import groovy.transform.Internal
 import org.apache.groovy.util.SystemUtil
@@ -32,6 +33,8 @@ import java.util.concurrent.ForkJoinWorkerThread
 import java.util.concurrent.TimeUnit
 import java.util.function.Function
 import java.util.function.Supplier
+import java.util.logging.Level
+import java.util.logging.Logger
 import java.util.stream.Collectors
 
 import static org.apache.groovy.ginq.provider.collection.runtime.Queryable.from
@@ -141,6 +144,7 @@ class QueryableHelper {
     private QueryableHelper() {}
 
     private static class ThreadPoolHolder {
+        private static final Logger LOGGER = Logger.getLogger(ThreadPoolHolder.class.getName());
         static int fjSeq
         static int seq
         static final int PARALLELISM = SystemUtil.getIntegerSafe("groovy.ginq.parallelism", Runtime.getRuntime().availableProcessors() + 1);
@@ -149,12 +153,27 @@ class QueryableHelper {
             worker.setName("ginq-fj-thread-${fjSeq++}")
             return worker
         }, null, false)
-        static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(PARALLELISM, (Runnable r) -> {
-            Thread t = new Thread(r)
-            t.setName("ginq-thread-${seq++}")
-            t.setDaemon(true)
-            return t
-        })
+        static final ExecutorService THREAD_POOL = createExecutorService()
+
+        @CompileDynamic
+        private static ExecutorService createExecutorService() {
+            var threadPool
+            try {
+                threadPool = Executors.newVirtualThreadPerTaskExecutor()
+            } catch (e) {
+                if (LOGGER.isLoggable(Level.FINE)) {
+                    LOGGER.fine("Failed to create VirtualThreadPerTaskExecutor.\n${e.asString()}")
+                }
+                threadPool = Executors.newFixedThreadPool(PARALLELISM, (Runnable r) -> {
+                    Thread t = new Thread(r)
+                    t.setName("ginq-thread-${seq++}")
+                    t.setDaemon(true)
+                    return t
+                })
+            }
+            return threadPool
+        }
+
         private ThreadPoolHolder() {}
     }
 }