Copilot commented on code in PR #8023: URL: https://github.com/apache/incubator-seata/pull/8023#discussion_r3026217946
########## threadpool/src/main/java/org/apache/seata/common/thread/ThreadPoolExecutorFactory.java: ########## @@ -0,0 +1,178 @@ +/* + * 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 org.apache.seata.common.thread; + +import org.apache.seata.common.loader.EnhancedServiceLoader; +import org.apache.seata.common.loader.EnhancedServiceNotFoundException; + +import java.util.Objects; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * Central factory used by Seata managed thread pools. + */ +public final class ThreadPoolExecutorFactory { + + private ThreadPoolExecutorFactory() {} + + public static ThreadFactory newThreadFactory(String threadPrefix, int totalSize) { + return newThreadFactory(threadPrefix, totalSize, true); + } + + public static ThreadFactory newThreadFactory(String threadPrefix, int totalSize, boolean daemon) { + Objects.requireNonNull(threadPrefix, "threadPrefix must not be null"); + return new NamedThreadFactory(threadPrefix, totalSize, daemon); + } + + public static ThreadPoolExecutor newThreadPoolExecutor( + String threadPrefix, + int corePoolSize, + int maximumPoolSize, + long keepAliveTime, + TimeUnit unit, + BlockingQueue<Runnable> workQueue) { + return newThreadPoolExecutor(threadPrefix, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, true); + } + + public static ThreadPoolExecutor newThreadPoolExecutor( + String threadPrefix, + int corePoolSize, + int maximumPoolSize, + long keepAliveTime, + TimeUnit unit, + BlockingQueue<Runnable> workQueue, + boolean daemon) { + return newThreadPoolExecutor( + threadPrefix, + corePoolSize, + maximumPoolSize, + keepAliveTime, + unit, + workQueue, + daemon, + new ThreadPoolExecutor.AbortPolicy()); + } + + public static ThreadPoolExecutor newThreadPoolExecutor( + String threadPrefix, + int corePoolSize, + int maximumPoolSize, + long keepAliveTime, + TimeUnit unit, + BlockingQueue<Runnable> workQueue, + RejectedExecutionHandler rejectedHandler) { + return newThreadPoolExecutor( + threadPrefix, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, true, rejectedHandler); + } + + public static ThreadPoolExecutor newThreadPoolExecutor( + String threadPrefix, + int corePoolSize, + int maximumPoolSize, + long keepAliveTime, + TimeUnit unit, + BlockingQueue<Runnable> workQueue, + boolean daemon, + RejectedExecutionHandler rejectedHandler) { + validateThreadPoolArguments(threadPrefix, corePoolSize, maximumPoolSize, keepAliveTime, unit); + return resolveThreadPoolProvider() + .newThreadPoolExecutor( + threadPrefix, + corePoolSize, + maximumPoolSize, + keepAliveTime, + unit, + Objects.requireNonNull(workQueue, "workQueue must not be null"), + daemon, + Objects.requireNonNull(rejectedHandler, "rejectedHandler must not be null")); + } + + public static ScheduledThreadPoolExecutor newScheduledThreadPoolExecutor(String threadPrefix, int corePoolSize) { + return newScheduledThreadPoolExecutor(threadPrefix, corePoolSize, true); + } + + public static ScheduledThreadPoolExecutor newScheduledThreadPoolExecutor( + String threadPrefix, int corePoolSize, boolean daemon) { + return newScheduledThreadPoolExecutor(threadPrefix, corePoolSize, daemon, new ThreadPoolExecutor.AbortPolicy()); + } + + public static ScheduledThreadPoolExecutor newScheduledThreadPoolExecutor( + String threadPrefix, int corePoolSize, boolean daemon, RejectedExecutionHandler rejectedHandler) { + validateScheduledThreadPoolArguments(threadPrefix, corePoolSize); + return resolveThreadPoolProvider() + .newScheduledThreadPoolExecutor( + threadPrefix, + corePoolSize, + daemon, + Objects.requireNonNull(rejectedHandler, "rejectedHandler must not be null")); + } + + private static void validateThreadPoolArguments( + String threadPrefix, int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit) { + Objects.requireNonNull(threadPrefix, "threadPrefix must not be null"); + Objects.requireNonNull(unit, "timeUnit must not be null"); + if (corePoolSize < 0) { Review Comment: Minor: the null-check message uses `"timeUnit must not be null"`, but the parameter name here is `unit`. Consider updating the message to match the argument name for clearer diagnostics. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
