From 6f5e5480cbe13bec9923579ea657e2874d7e6dd3 Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Mon, 15 Jul 2024 22:02:07 +0200 Subject: [PATCH] LDEV-5020 - use virtual thread with each caller functions --- .../java/lucee/commons/io/SystemUtil.java | 60 ++++++++++--------- .../lucee/runtime/functions/closure/Each.java | 4 +- .../runtime/functions/closure/Every.java | 4 +- .../runtime/functions/closure/Filter.java | 4 +- .../lucee/runtime/functions/closure/Map.java | 4 +- .../lucee/runtime/functions/closure/Some.java | 4 +- .../java/lucee/runtime/mvn/MavenUtil.java | 4 +- .../java/lucee/runtime/thread/ThreadUtil.java | 23 ++++++- loader/build.xml | 2 +- loader/pom.xml | 2 +- 10 files changed, 68 insertions(+), 43 deletions(-) diff --git a/core/src/main/java/lucee/commons/io/SystemUtil.java b/core/src/main/java/lucee/commons/io/SystemUtil.java index de7f649566..d9f34cea04 100644 --- a/core/src/main/java/lucee/commons/io/SystemUtil.java +++ b/core/src/main/java/lucee/commons/io/SystemUtil.java @@ -163,6 +163,12 @@ public final class SystemUtil { public static final int JAVA_VERSION_27 = 27; // FUTURE lucee.runtime.util.SystemUtil.JAVA_VERSION_14; public static final int JAVA_VERSION_28 = 28; // FUTURE lucee.runtime.util.SystemUtil.JAVA_VERSION_14; public static final int JAVA_VERSION_29 = 29; // FUTURE lucee.runtime.util.SystemUtil.JAVA_VERSION_14; + public static final int JAVA_VERSION_30 = 30; + public static final int JAVA_VERSION_31 = 31; + public static final int JAVA_VERSION_32 = 32; + public static final int JAVA_VERSION_33 = 33; + public static final int JAVA_VERSION_34 = 34; + public static final int JAVA_VERSION_35 = 35; public static final int OUT = lucee.runtime.util.SystemUtil.OUT; public static final int ERR = lucee.runtime.util.SystemUtil.ERR; @@ -223,39 +229,37 @@ public final class SystemUtil { MemoryPoolMXBean tmp = getPermGenSpaceBean(); if (tmp != permGenSpaceBean) permGenSpaceBean = null; - if (JAVA_VERSION_STRING.startsWith("1.14.") || JAVA_VERSION_STRING.startsWith("14")) JAVA_VERSION = JAVA_VERSION_14; + if (JAVA_VERSION_STRING.startsWith("35.")) JAVA_VERSION = JAVA_VERSION_35; + else if (JAVA_VERSION_STRING.startsWith("34.")) JAVA_VERSION = JAVA_VERSION_34; + else if (JAVA_VERSION_STRING.startsWith("33.")) JAVA_VERSION = JAVA_VERSION_33; + else if (JAVA_VERSION_STRING.startsWith("32.")) JAVA_VERSION = JAVA_VERSION_32; + else if (JAVA_VERSION_STRING.startsWith("31.")) JAVA_VERSION = JAVA_VERSION_31; + else if (JAVA_VERSION_STRING.startsWith("30.")) JAVA_VERSION = JAVA_VERSION_30; + else if (JAVA_VERSION_STRING.startsWith("29.")) JAVA_VERSION = JAVA_VERSION_29; + else if (JAVA_VERSION_STRING.startsWith("28.")) JAVA_VERSION = JAVA_VERSION_28; + else if (JAVA_VERSION_STRING.startsWith("27.")) JAVA_VERSION = JAVA_VERSION_27; + else if (JAVA_VERSION_STRING.startsWith("26.")) JAVA_VERSION = JAVA_VERSION_26; + else if (JAVA_VERSION_STRING.startsWith("25.")) JAVA_VERSION = JAVA_VERSION_25; + else if (JAVA_VERSION_STRING.startsWith("24.")) JAVA_VERSION = JAVA_VERSION_24; + else if (JAVA_VERSION_STRING.startsWith("23.")) JAVA_VERSION = JAVA_VERSION_23; + else if (JAVA_VERSION_STRING.startsWith("22.")) JAVA_VERSION = JAVA_VERSION_22; + else if (JAVA_VERSION_STRING.startsWith("21.")) JAVA_VERSION = JAVA_VERSION_21; + else if (JAVA_VERSION_STRING.startsWith("20.")) JAVA_VERSION = JAVA_VERSION_20; + else if (JAVA_VERSION_STRING.startsWith("19.")) JAVA_VERSION = JAVA_VERSION_19; + else if (JAVA_VERSION_STRING.startsWith("18.")) JAVA_VERSION = JAVA_VERSION_18; + else if (JAVA_VERSION_STRING.startsWith("17.")) JAVA_VERSION = JAVA_VERSION_17; + else if (JAVA_VERSION_STRING.startsWith("16.")) JAVA_VERSION = JAVA_VERSION_16; + else if (JAVA_VERSION_STRING.startsWith("15.")) JAVA_VERSION = JAVA_VERSION_15; + + else if (JAVA_VERSION_STRING.startsWith("1.14.") || JAVA_VERSION_STRING.startsWith("14")) JAVA_VERSION = JAVA_VERSION_14; else if (JAVA_VERSION_STRING.startsWith("1.13.") || JAVA_VERSION_STRING.startsWith("13")) JAVA_VERSION = JAVA_VERSION_13; else if (JAVA_VERSION_STRING.startsWith("1.12.") || JAVA_VERSION_STRING.startsWith("12")) JAVA_VERSION = JAVA_VERSION_12; else if (JAVA_VERSION_STRING.startsWith("1.11.") || JAVA_VERSION_STRING.startsWith("11")) JAVA_VERSION = JAVA_VERSION_11; else if (JAVA_VERSION_STRING.startsWith("1.10.") || JAVA_VERSION_STRING.startsWith("10")) JAVA_VERSION = JAVA_VERSION_10; else if (JAVA_VERSION_STRING.startsWith("1.9.") || JAVA_VERSION_STRING.startsWith("9.")) JAVA_VERSION = JAVA_VERSION_9; - else if (JAVA_VERSION_STRING.startsWith("1.8.")) JAVA_VERSION = JAVA_VERSION_8; - else if (JAVA_VERSION_STRING.startsWith("1.7.")) JAVA_VERSION = JAVA_VERSION_7; - else if (JAVA_VERSION_STRING.startsWith("1.6.")) JAVA_VERSION = JAVA_VERSION_6; - else if (JAVA_VERSION_STRING.startsWith("1.6.")) JAVA_VERSION = JAVA_VERSION_6; - - else if (JAVA_VERSION_STRING.startsWith("8.")) JAVA_VERSION = JAVA_VERSION_8; - else if (JAVA_VERSION_STRING.startsWith("9.")) JAVA_VERSION = JAVA_VERSION_9; - else if (JAVA_VERSION_STRING.startsWith("10.")) JAVA_VERSION = JAVA_VERSION_10; - else if (JAVA_VERSION_STRING.startsWith("11.")) JAVA_VERSION = JAVA_VERSION_11; - else if (JAVA_VERSION_STRING.startsWith("12.")) JAVA_VERSION = JAVA_VERSION_12; - else if (JAVA_VERSION_STRING.startsWith("13.")) JAVA_VERSION = JAVA_VERSION_13; - else if (JAVA_VERSION_STRING.startsWith("14.")) JAVA_VERSION = JAVA_VERSION_14; - else if (JAVA_VERSION_STRING.startsWith("15.")) JAVA_VERSION = JAVA_VERSION_15; - else if (JAVA_VERSION_STRING.startsWith("16.")) JAVA_VERSION = JAVA_VERSION_16; - else if (JAVA_VERSION_STRING.startsWith("17.")) JAVA_VERSION = JAVA_VERSION_17; - else if (JAVA_VERSION_STRING.startsWith("18.")) JAVA_VERSION = JAVA_VERSION_18; - else if (JAVA_VERSION_STRING.startsWith("19.")) JAVA_VERSION = JAVA_VERSION_19; - else if (JAVA_VERSION_STRING.startsWith("20.")) JAVA_VERSION = JAVA_VERSION_20; - else if (JAVA_VERSION_STRING.startsWith("21.")) JAVA_VERSION = JAVA_VERSION_21; - else if (JAVA_VERSION_STRING.startsWith("22.")) JAVA_VERSION = JAVA_VERSION_22; - else if (JAVA_VERSION_STRING.startsWith("23.")) JAVA_VERSION = JAVA_VERSION_23; - else if (JAVA_VERSION_STRING.startsWith("24.")) JAVA_VERSION = JAVA_VERSION_24; - else if (JAVA_VERSION_STRING.startsWith("25.")) JAVA_VERSION = JAVA_VERSION_25; - else if (JAVA_VERSION_STRING.startsWith("26.")) JAVA_VERSION = JAVA_VERSION_26; - else if (JAVA_VERSION_STRING.startsWith("27.")) JAVA_VERSION = JAVA_VERSION_27; - else if (JAVA_VERSION_STRING.startsWith("28.")) JAVA_VERSION = JAVA_VERSION_28; - else if (JAVA_VERSION_STRING.startsWith("29.")) JAVA_VERSION = JAVA_VERSION_29; + else if (JAVA_VERSION_STRING.startsWith("1.8.") || JAVA_VERSION_STRING.startsWith("8.")) JAVA_VERSION = JAVA_VERSION_8; + else if (JAVA_VERSION_STRING.startsWith("1.7.") || JAVA_VERSION_STRING.startsWith("7.")) JAVA_VERSION = JAVA_VERSION_7; + else if (JAVA_VERSION_STRING.startsWith("1.6.") || JAVA_VERSION_STRING.startsWith("6.")) JAVA_VERSION = JAVA_VERSION_6; else JAVA_VERSION = 0; } diff --git a/core/src/main/java/lucee/runtime/functions/closure/Each.java b/core/src/main/java/lucee/runtime/functions/closure/Each.java index 5daf8ccefa..ccc26687b5 100644 --- a/core/src/main/java/lucee/runtime/functions/closure/Each.java +++ b/core/src/main/java/lucee/runtime/functions/closure/Each.java @@ -29,7 +29,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import lucee.runtime.PageContext; @@ -40,6 +39,7 @@ import lucee.runtime.exp.ParentException; import lucee.runtime.ext.function.BIF; import lucee.runtime.op.Caster; +import lucee.runtime.thread.ThreadUtil; import lucee.runtime.type.Array; import lucee.runtime.type.ArrayPro; import lucee.runtime.type.Collection.Key; @@ -77,7 +77,7 @@ private static String _call(PageContext pc, Object obj, UDF udf, boolean paralle else if (maxThreads == 1) parallel = false; if (parallel) { - execute = Executors.newFixedThreadPool(maxThreads); + execute = ThreadUtil.createExecutorService(maxThreads); futures = new ArrayList>>(); } diff --git a/core/src/main/java/lucee/runtime/functions/closure/Every.java b/core/src/main/java/lucee/runtime/functions/closure/Every.java index 635b4cf9db..e71c00b000 100644 --- a/core/src/main/java/lucee/runtime/functions/closure/Every.java +++ b/core/src/main/java/lucee/runtime/functions/closure/Every.java @@ -25,7 +25,6 @@ import java.util.ListIterator; import java.util.Map.Entry; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import lucee.runtime.PageContext; @@ -37,6 +36,7 @@ import lucee.runtime.exp.ParentException; import lucee.runtime.ext.function.BIF; import lucee.runtime.op.Caster; +import lucee.runtime.thread.ThreadUtil; import lucee.runtime.type.Array; import lucee.runtime.type.ArrayPro; import lucee.runtime.type.Collection.Key; @@ -80,7 +80,7 @@ private static boolean _call(PageContext pc, Object obj, UDF udf, boolean parall else if (maxThreads == 1) parallel = false; if (parallel) { - execute = Executors.newFixedThreadPool(maxThreads); + execute = ThreadUtil.createExecutorService(maxThreads); futures = new ArrayList>>(); } diff --git a/core/src/main/java/lucee/runtime/functions/closure/Filter.java b/core/src/main/java/lucee/runtime/functions/closure/Filter.java index 4f29311408..653a23721a 100644 --- a/core/src/main/java/lucee/runtime/functions/closure/Filter.java +++ b/core/src/main/java/lucee/runtime/functions/closure/Filter.java @@ -25,7 +25,6 @@ import java.util.ListIterator; import java.util.Map.Entry; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import lucee.commons.lang.Pair; @@ -38,6 +37,7 @@ import lucee.runtime.exp.ParentException; import lucee.runtime.ext.function.BIF; import lucee.runtime.op.Caster; +import lucee.runtime.thread.ThreadUtil; import lucee.runtime.type.Array; import lucee.runtime.type.ArrayImpl; import lucee.runtime.type.ArrayPro; @@ -85,7 +85,7 @@ private static Collection _call(PageContext pc, Object obj, UDF udf, boolean par // 1 == not parallel else if (maxThreads == 1) parallel = false; if (parallel) { - execute = Executors.newFixedThreadPool(maxThreads); + execute = ThreadUtil.createExecutorService(maxThreads); futures = new ArrayList>>>(); } diff --git a/core/src/main/java/lucee/runtime/functions/closure/Map.java b/core/src/main/java/lucee/runtime/functions/closure/Map.java index b3694c3e3d..d8970cb573 100644 --- a/core/src/main/java/lucee/runtime/functions/closure/Map.java +++ b/core/src/main/java/lucee/runtime/functions/closure/Map.java @@ -26,7 +26,6 @@ import java.util.ListIterator; import java.util.Map.Entry; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import lucee.runtime.PageContext; @@ -38,6 +37,7 @@ import lucee.runtime.exp.ParentException; import lucee.runtime.ext.function.BIF; import lucee.runtime.op.Caster; +import lucee.runtime.thread.ThreadUtil; import lucee.runtime.type.Array; import lucee.runtime.type.ArrayImpl; import lucee.runtime.type.ArrayPro; @@ -85,7 +85,7 @@ private static Collection _call(PageContext pc, Object obj, UDF udf, boolean par // 1 == not parallel else if (maxThreads == 1) parallel = false; if (parallel) { - execute = Executors.newFixedThreadPool(maxThreads); + execute = ThreadUtil.createExecutorService(maxThreads); futures = new ArrayList>>(); } diff --git a/core/src/main/java/lucee/runtime/functions/closure/Some.java b/core/src/main/java/lucee/runtime/functions/closure/Some.java index 592fa9d118..1d64c63119 100644 --- a/core/src/main/java/lucee/runtime/functions/closure/Some.java +++ b/core/src/main/java/lucee/runtime/functions/closure/Some.java @@ -25,7 +25,6 @@ import java.util.ListIterator; import java.util.Map.Entry; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import lucee.runtime.PageContext; @@ -37,6 +36,7 @@ import lucee.runtime.exp.ParentException; import lucee.runtime.ext.function.BIF; import lucee.runtime.op.Caster; +import lucee.runtime.thread.ThreadUtil; import lucee.runtime.type.Array; import lucee.runtime.type.ArrayPro; import lucee.runtime.type.Collection.Key; @@ -79,7 +79,7 @@ private static boolean _call(PageContext pc, Object obj, UDF udf, boolean parall // 1 == not parallel else if (maxThreads == 1) parallel = false; if (parallel) { - execute = Executors.newFixedThreadPool(maxThreads); + execute = ThreadUtil.createExecutorService(maxThreads); futures = new ArrayList>>(); } diff --git a/core/src/main/java/lucee/runtime/mvn/MavenUtil.java b/core/src/main/java/lucee/runtime/mvn/MavenUtil.java index 8553596c2d..0f1670173e 100644 --- a/core/src/main/java/lucee/runtime/mvn/MavenUtil.java +++ b/core/src/main/java/lucee/runtime/mvn/MavenUtil.java @@ -12,7 +12,6 @@ import java.util.Map.Entry; import java.util.Properties; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.apache.http.HttpEntity; @@ -29,6 +28,7 @@ import lucee.commons.lang.ExceptionUtil; import lucee.commons.lang.SerializableObject; import lucee.runtime.mvn.POMReader.Dependency; +import lucee.runtime.thread.ThreadUtil; import lucee.runtime.type.util.ListUtil; public class MavenUtil { @@ -109,7 +109,7 @@ public static List getDependencies(List rawDependenci List dependencies = new ArrayList<>(); List parentDendencyManagement = null; - ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + ExecutorService executor = ThreadUtil.createExecutorService(Runtime.getRuntime().availableProcessors()); if (parent != null) { parentDendencyManagement = current.getDependencyManagement(); diff --git a/core/src/main/java/lucee/runtime/thread/ThreadUtil.java b/core/src/main/java/lucee/runtime/thread/ThreadUtil.java index d9d9522f9e..a76a6ffdf9 100755 --- a/core/src/main/java/lucee/runtime/thread/ThreadUtil.java +++ b/core/src/main/java/lucee/runtime/thread/ThreadUtil.java @@ -19,8 +19,13 @@ package lucee.runtime.thread; import java.io.OutputStream; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; import java.util.Iterator; import java.util.Map.Entry; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -29,6 +34,7 @@ import lucee.aprint; import lucee.commons.io.DevNullOutputStream; +import lucee.commons.io.SystemUtil; import lucee.commons.io.res.Resource; import lucee.commons.lang.ExceptionUtil; import lucee.commons.lang.Pair; @@ -58,7 +64,6 @@ public static PageContextImpl clonePageContext(PageContext pc, OutputStream os, PageContextImpl pci = (PageContextImpl) pc; PageContextImpl dest = factory.getPageContextImpl(factory.getServlet(), req, rsp, null, false, -1, false, register2Thread, true, pc.getRequestTimeout(), register2RunningThreads, false, false, stateless ? null : pci); - // pci.copyStateTo(dest); return dest; } @@ -202,4 +207,20 @@ public static boolean isInNativeMethod(Thread thread, boolean defaultValue) { StackTraceElement ste = stes[0]; return ste.isNativeMethod(); } + + public static ExecutorService createExecutorService(int maxThreads) { + if (SystemUtil.JAVA_VERSION >= SystemUtil.JAVA_VERSION_19) { + // FUTURE use newVirtualThreadPerTaskExecutor natively + try { + MethodHandles.Lookup lookup = MethodHandles.lookup(); + MethodType methodType = MethodType.methodType(ExecutorService.class); + MethodHandle methodHandle = lookup.findStatic(Executors.class, "newVirtualThreadPerTaskExecutor", methodType); + return (ExecutorService) methodHandle.invoke(); + } + catch (Throwable e) { + ExceptionUtil.rethrowIfNecessary(e); + } + } + return Executors.newFixedThreadPool(maxThreads); + } } \ No newline at end of file diff --git a/loader/build.xml b/loader/build.xml index 769a1345be..5d4ad93f17 100644 --- a/loader/build.xml +++ b/loader/build.xml @@ -2,7 +2,7 @@ - + diff --git a/loader/pom.xml b/loader/pom.xml index af71184fb6..006e6d6528 100644 --- a/loader/pom.xml +++ b/loader/pom.xml @@ -3,7 +3,7 @@ org.lucee lucee - 6.2.0.16-SNAPSHOT + 6.2.0.17-SNAPSHOT jar Lucee Loader Build