From 93513dea4399651270096c2a7f8b5294035f9452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CHzjNeverStop=E2=80=9D?= <“441627022@qq.com”> Date: Tue, 12 Dec 2023 11:13:25 +0800 Subject: [PATCH] 1. update sofa-common-tools 1.3.15 2. add SofaVirtualThreadFactory api for low java version sdk --- .../virtual/SofaVirtualThreadFactory.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/main/java/com/alipay/sofa/common/thread/virtual/SofaVirtualThreadFactory.java diff --git a/src/main/java/com/alipay/sofa/common/thread/virtual/SofaVirtualThreadFactory.java b/src/main/java/com/alipay/sofa/common/thread/virtual/SofaVirtualThreadFactory.java new file mode 100644 index 0000000..14d4018 --- /dev/null +++ b/src/main/java/com/alipay/sofa/common/thread/virtual/SofaVirtualThreadFactory.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.common.thread.virtual; + +import java.util.concurrent.ExecutorService; + +/** + * Delegate for virtual thread handling on JDK 21. + * This is a dummy version for reachability on JDK less than 21. + * + * @author huzijie + * @version SofaVirtualThreadFactory.java, v 0.1 2023年11月20日 3:57 PM huzijie Exp $ + */ +public class SofaVirtualThreadFactory { + + /** + * Create a virtual thread with name. + * + * @param name thread name + * @param runnable task to run with thread + * @return a virtual thread with runnable + * @since 2.1.0 + */ + public static Thread ofThread(String name, Runnable runnable) { + throw new UnsupportedOperationException("Virtual threads not supported on JDK <21"); + } + + /** + * Create a virtual thread with name. + * + * @param name thread name + * @param inheritInheritableThreadLocals {@code true} to inherit, {@code false} to not inherit + * @param uncaughtExceptionHandler uncaught exception handler + * @param runnable task to run with thread + * @return a virtual thread with runnable + * @since 2.1.0 + */ + + public static Thread ofThread(String name, boolean inheritInheritableThreadLocals, + Thread.UncaughtExceptionHandler uncaughtExceptionHandler, + Runnable runnable) { + throw new UnsupportedOperationException("Virtual threads not supported on JDK <21"); + } + + /** + * Creates an Executor that starts a new virtual Thread for each task. + * The number of threads created by the Executor is unbounded. + * + * @param prefix thread prefix + * @return a new executor that creates a new virtual Thread for each task + * @since 2.1.0 + */ + public static ExecutorService ofExecutorService(String prefix) { + throw new UnsupportedOperationException("Virtual threads not supported on JDK <21"); + } + + /** + * Creates an Executor that starts a new virtual Thread for each task. + * The number of threads created by the Executor is unbounded. + * + * @param prefix thread prefix + * @param start the starting value of the counter + * @param inheritInheritableThreadLocals {@code true} to inherit, {@code false} to not inherit + * @param uncaughtExceptionHandler uncaught exception handler + * @return a new executor that creates a new virtual Thread for each task + * @since 2.1.0 + */ + public static ExecutorService ofExecutorService(String prefix, + long start, + boolean inheritInheritableThreadLocals, + Thread.UncaughtExceptionHandler uncaughtExceptionHandler) { + throw new UnsupportedOperationException("Virtual threads not supported on JDK <21"); + } +}