From a8fdbbc50c05eb559900fba7545a1bb89e595518 Mon Sep 17 00:00:00 2001 From: weihu Date: Sun, 2 Oct 2022 15:30:19 +0800 Subject: [PATCH] Simplified thread pool creation --- .../common/function/MatcherFunctionTest.java | 2 +- .../executor/support/ThreadPoolBuilder.java | 26 ++++++++++++++++--- .../config/etcd/config/ThreadPoolConfig.java | 12 +++------ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/function/MatcherFunctionTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/function/MatcherFunctionTest.java index 2a202302..93287ed2 100644 --- a/hippo4j-common/src/test/java/cn/hippo4j/common/function/MatcherFunctionTest.java +++ b/hippo4j-common/src/test/java/cn/hippo4j/common/function/MatcherFunctionTest.java @@ -22,7 +22,7 @@ import java.math.BigDecimal; import org.junit.Test; public final class MatcherFunctionTest { - + public static boolean matchTest(Matcher matcher, T value) { return matcher.match(value); } diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadPoolBuilder.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadPoolBuilder.java index 1aa069c4..677e8ea0 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadPoolBuilder.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/support/ThreadPoolBuilder.java @@ -17,14 +17,19 @@ package cn.hippo4j.core.executor.support; +import java.math.BigDecimal; +import java.util.Optional; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + import cn.hippo4j.common.design.builder.Builder; import cn.hippo4j.common.executor.support.BlockingQueueTypeEnum; import cn.hippo4j.common.toolkit.Assert; -import org.springframework.core.task.TaskDecorator; -import java.math.BigDecimal; -import java.util.Optional; -import java.util.concurrent.*; +import org.springframework.core.task.TaskDecorator; /** * Thread-pool builder. @@ -221,6 +226,19 @@ public class ThreadPoolBuilder implements Builder { return new ThreadPoolBuilder(); } + /** + * Create thread pool by thread pool id + * @param threadPoolId threadPoolId + * @return ThreadPoolExecutor + */ + public static ThreadPoolExecutor builderById(String threadPoolId) { + return ThreadPoolBuilder.builder() + .threadFactory(threadPoolId) + .threadPoolId(threadPoolId) + .dynamicPool() + .build(); + } + private static ThreadPoolExecutor buildPool(ThreadPoolBuilder builder) { return AbstractBuildThreadPoolTemplate.buildPool(buildInitParam(builder)); } diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java index 71e6d943..c6cf9c50 100644 --- a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java @@ -17,13 +17,14 @@ package cn.hippo4j.example.config.etcd.config; +import java.util.concurrent.ThreadPoolExecutor; + import cn.hippo4j.core.executor.DynamicThreadPool; import cn.hippo4j.core.executor.support.ThreadPoolBuilder; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import java.util.concurrent.ThreadPoolExecutor; - /** * @author : wh * @date : 2022/9/2 19:26 @@ -36,11 +37,6 @@ public class ThreadPoolConfig { @DynamicThreadPool public ThreadPoolExecutor messageConsumeDynamicExecutor() { String threadPoolId = "message-consume"; - ThreadPoolExecutor messageConsumeDynamicExecutor = ThreadPoolBuilder.builder() - .threadFactory(threadPoolId) - .threadPoolId(threadPoolId) - .dynamicPool() - .build(); - return messageConsumeDynamicExecutor; + return ThreadPoolBuilder.builderById(threadPoolId); } }