|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.amazon.sns.messaging.lib.concurrent; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.concurrent.ExecutorService; |
| 21 | +import java.util.concurrent.Executors; |
| 22 | +import java.util.function.Supplier; |
| 23 | + |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +import lombok.AccessLevel; |
| 28 | +import lombok.NoArgsConstructor; |
| 29 | +import lombok.SneakyThrows; |
| 30 | + |
| 31 | +@NoArgsConstructor(access = AccessLevel.PRIVATE) |
| 32 | +public final class ExecutorsProvider { |
| 33 | + |
| 34 | + private static final Logger LOGGER = LoggerFactory.getLogger(ExecutorsProvider.class); |
| 35 | + |
| 36 | + private static Supplier<ExecutorService> supplierExecutorService; |
| 37 | + |
| 38 | + static { |
| 39 | + if (ExecutorsProvider.getJavaVersion() >= 21) { |
| 40 | + ExecutorsProvider.supplierExecutorService = ExecutorsProvider::getVirtualExecutorService; |
| 41 | + ExecutorsProvider.LOGGER.info("Java version is {}, using virtual thread executor", ExecutorsProvider.getJavaVersion()); |
| 42 | + } else { |
| 43 | + ExecutorsProvider.supplierExecutorService = ExecutorsProvider::getDefaultExecutorService; |
| 44 | + ExecutorsProvider.LOGGER.info("Java version is {}, using default thread executor", ExecutorsProvider.getJavaVersion()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static ExecutorService getExecutorService() { |
| 49 | + return ExecutorsProvider.supplierExecutorService.get(); |
| 50 | + } |
| 51 | + |
| 52 | + @SneakyThrows |
| 53 | + private static ExecutorService getDefaultExecutorService() { |
| 54 | + return Executors.newSingleThreadExecutor(); |
| 55 | + } |
| 56 | + |
| 57 | + @SneakyThrows |
| 58 | + private static ExecutorService getVirtualExecutorService() { |
| 59 | + final Class<?> clazzThread = Executors.class; |
| 60 | + final Method ofVirtualMethod = clazzThread.getMethod("newVirtualThreadPerTaskExecutor"); |
| 61 | + return ExecutorService.class.cast(ofVirtualMethod.invoke(null)); |
| 62 | + } |
| 63 | + |
| 64 | + private static int getJavaVersion() { |
| 65 | + String version = System.getProperty("java.version"); |
| 66 | + |
| 67 | + if (version.startsWith("1.")) { |
| 68 | + version = version.substring(2); |
| 69 | + } |
| 70 | + |
| 71 | + final int dotPos = version.indexOf('.'); |
| 72 | + final int dashPos = version.indexOf('-'); |
| 73 | + final int endIndex = dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1; |
| 74 | + |
| 75 | + return Integer.parseInt(version.substring(0, endIndex)); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments