From 6473a4f3f40b29866826a4d53542a47f2b0658dd Mon Sep 17 00:00:00 2001 From: Tomas Langer Date: Mon, 31 Jul 2023 14:09:58 +0200 Subject: [PATCH] Remove VirtualExecutorUtil.java (#7263) --- .../common/configurable/ObserverManager.java | 6 +- .../configurable/ThreadPoolSupplier.java | 20 ++-- .../configurable/VirtualExecutorUtil.java | 100 ------------------ 3 files changed, 10 insertions(+), 116 deletions(-) delete mode 100644 common/configurable/src/main/java/io/helidon/common/configurable/VirtualExecutorUtil.java diff --git a/common/configurable/src/main/java/io/helidon/common/configurable/ObserverManager.java b/common/configurable/src/main/java/io/helidon/common/configurable/ObserverManager.java index 90aaf8d1c7a..f7693e1fd02 100644 --- a/common/configurable/src/main/java/io/helidon/common/configurable/ObserverManager.java +++ b/common/configurable/src/main/java/io/helidon/common/configurable/ObserverManager.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Oracle and/or its affiliates. + * Copyright (c) 2022, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package io.helidon.common.configurable; import java.lang.System.Logger.Level; @@ -22,6 +23,7 @@ import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -216,7 +218,7 @@ private static class MethodInvocationImpl implements ExecutorServiceSupplierObse private final Class type; private static final LazyValue VIRTUAL_EXECUTOR_SERVICE = LazyValue - .create(VirtualExecutorUtil::executorService); + .create(Executors::newVirtualThreadPerTaskExecutor); static MethodInvocationImpl create(String displayName, String description, String methodName) { ExecutorService executorService = VIRTUAL_EXECUTOR_SERVICE.get(); diff --git a/common/configurable/src/main/java/io/helidon/common/configurable/ThreadPoolSupplier.java b/common/configurable/src/main/java/io/helidon/common/configurable/ThreadPoolSupplier.java index bf31ac91063..4eb6707831e 100644 --- a/common/configurable/src/main/java/io/helidon/common/configurable/ThreadPoolSupplier.java +++ b/common/configurable/src/main/java/io/helidon/common/configurable/ThreadPoolSupplier.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2022 Oracle and/or its affiliates. + * Copyright (c) 2018, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,8 @@ package io.helidon.common.configurable; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -72,7 +74,7 @@ private ThreadPoolSupplier(Builder builder) { this.growthThreshold = builder.growthThreshold; this.growthRate = builder.growthRate; this.rejectionHandler = builder.rejectionHandler == null ? DEFAULT_REJECTION_POLICY : builder.rejectionHandler; - this.useVirtualThreads = builder.useVirtualThreads || builder.virtualThreadsEnforced; + this.useVirtualThreads = builder.useVirtualThreads; ObserverManager.registerSupplier(this, name, "general", useVirtualThreads); } @@ -111,10 +113,8 @@ public static ThreadPoolSupplier create(String name) { ExecutorService getThreadPool() { if (useVirtualThreads) { - if (VirtualExecutorUtil.isVirtualSupported()) { - LOGGER.log(System.Logger.Level.TRACE, "Using unbounded virtual executor service for pool " + name); - return ObserverManager.registerExecutorService(this, VirtualExecutorUtil.executorService()); - } + ThreadFactory factory = Thread.ofVirtual().name(name + "-", 0).factory(); + return ObserverManager.registerExecutorService(this, Executors.newThreadPerTaskExecutor(factory)); } ThreadPool result = ThreadPool.create(name, @@ -165,7 +165,6 @@ public static final class Builder implements io.helidon.common.Builder SUPPORTED = LazyValue.create(VirtualExecutorUtil::findSupported); - private static final LazyValue EXECUTOR_SERVICE = LazyValue.create(VirtualExecutorUtil::findExecutor); - // newer and older builds - private static final List SUPPORTED_METHOD_NAMES = List.of("newVirtualThreadPerTaskExecutor", - "newVirtualThreadExecutor"); - - private VirtualExecutorUtil() { - } - - static boolean isVirtualSupported() { - return SUPPORTED.get(); - } - - static ExecutorService executorService() { - ExecutorService result = EXECUTOR_SERVICE.get(); - if (result == null) { - throw new IllegalStateException("Virtual executor service is not supported on this JVM"); - } - return result; - } - - private static boolean findSupported() { - try { - // the method is intentionally NOT CACHED in static context, to support differences between build - // and runtime environments (support for GraalVM native image) - findMethod(); - return true; - } catch (final ReflectiveOperationException e) { - LOGGER.log(Level.DEBUG, "Loom virtual executor service not available", e); - } - - return false; - } - - private static ExecutorService findExecutor() { - try { - // the method is intentionally NOT CACHED in static context, to support differences between build - // and runtime environments (support for GraalVM native image) - return (ExecutorService) findMethod().invoke(null); - } catch (final ReflectiveOperationException e) { - LOGGER.log(Level.DEBUG, "Loom virtual executor service not available", e); - } - - return null; - } - - private static Method findMethod() throws ReflectiveOperationException { - ReflectiveOperationException previous = null; - - for (String methodName : SUPPORTED_METHOD_NAMES) { - try { - if (LOGGER.isLoggable(Level.DEBUG)) { - LOGGER.log(Level.DEBUG, "Trying Loom executor method Executors." + methodName + "()"); - } - return Executors.class.getDeclaredMethod(methodName); - } catch (ReflectiveOperationException e) { - if (previous == null) { - previous = e; - } else { - previous.addSuppressed(e); - } - } - } - if (previous == null) { - throw new NoSuchMethodException("Invalid Loom configuration, no methods defined."); - } - throw previous; - } -}