diff --git a/milky-common/pom.xml b/milky-common/pom.xml index 7b9303bf..d9f17d4f 100644 --- a/milky-common/pom.xml +++ b/milky-common/pom.xml @@ -27,6 +27,11 @@ disruptor + + com.google.guava + guava + + com.fasterxml.jackson.core jackson-databind @@ -44,12 +49,6 @@ jackson-dataformat-properties - - com.google.guava - guava - 30.1.1-jre - test - cn.sliew milky-test diff --git a/milky-common/src/main/java/cn/sliew/milky/common/concurrent/AbstractLoopRunnable.java b/milky-common/src/main/java/cn/sliew/milky/common/concurrent/AbstractLoopRunnable.java new file mode 100644 index 00000000..867f4363 --- /dev/null +++ b/milky-common/src/main/java/cn/sliew/milky/common/concurrent/AbstractLoopRunnable.java @@ -0,0 +1,16 @@ +package cn.sliew.milky.common.concurrent; + +public abstract class AbstractLoopRunnable implements LoopRunnable { + + protected volatile boolean terminal = false; + + @Override + public void terminate() { + terminal = true; + } + + @Override + public boolean isTerminated() { + return terminal; + } +} diff --git a/milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/DaemonThreadFactory.java b/milky-common/src/main/java/cn/sliew/milky/common/concurrent/DaemonThreadFactory.java similarity index 88% rename from milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/DaemonThreadFactory.java rename to milky-common/src/main/java/cn/sliew/milky/common/concurrent/DaemonThreadFactory.java index d6452d6a..50192cdf 100644 --- a/milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/DaemonThreadFactory.java +++ b/milky-common/src/main/java/cn/sliew/milky/common/concurrent/DaemonThreadFactory.java @@ -1,14 +1,11 @@ -package cn.sliew.milky.concurrent.thread; +package cn.sliew.milky.common.concurrent; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; import static cn.sliew.milky.common.check.Ensures.checkNotNull; -/** - * todo thread context。当创建线程的时候初始化线程的ThreadContext?? - */ -class DaemonThreadFactory implements ThreadFactory { +public class DaemonThreadFactory implements ThreadFactory { private final String threadNamePrefix; private final int threadPriority; @@ -22,7 +19,7 @@ public DaemonThreadFactory() { this(null); } - DaemonThreadFactory(String threadNamePrefix) { + public DaemonThreadFactory(String threadNamePrefix) { this(threadNamePrefix, true); } @@ -30,7 +27,7 @@ public DaemonThreadFactory(String threadNamePrefix, int threadPriority) { this(threadNamePrefix, threadPriority, true); } - DaemonThreadFactory(String threadNamePrefix, boolean daemon) { + public DaemonThreadFactory(String threadNamePrefix, boolean daemon) { this(threadNamePrefix, Thread.NORM_PRIORITY, daemon); } diff --git a/milky-common/src/main/java/cn/sliew/milky/common/concurrent/LoopRunnable.java b/milky-common/src/main/java/cn/sliew/milky/common/concurrent/LoopRunnable.java new file mode 100644 index 00000000..e6439b95 --- /dev/null +++ b/milky-common/src/main/java/cn/sliew/milky/common/concurrent/LoopRunnable.java @@ -0,0 +1,17 @@ +package cn.sliew.milky.common.concurrent; + +public interface LoopRunnable extends Runnable { + + void execute(); + + void terminate(); + + boolean isTerminated(); + + @Override + default void run() { + while (isTerminated() == false) { + execute(); + } + } +} diff --git a/milky-common/src/main/java/cn/sliew/milky/common/concurrent/ThreadPoolUtil.java b/milky-common/src/main/java/cn/sliew/milky/common/concurrent/ThreadPoolUtil.java new file mode 100644 index 00000000..e0647038 --- /dev/null +++ b/milky-common/src/main/java/cn/sliew/milky/common/concurrent/ThreadPoolUtil.java @@ -0,0 +1,28 @@ +package cn.sliew.milky.common.concurrent; + +import java.util.concurrent.*; + +public enum ThreadPoolUtil { + ; + + private static ConcurrentMap executors = new ConcurrentHashMap<>(); + + private static ExecutorService init(String poolName, int poolSize) { + return new ThreadPoolExecutor(poolSize, poolSize, + 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue(), + new DaemonThreadFactory("Pool-" + poolName, true), + new ThreadPoolExecutor.CallerRunsPolicy()); + } + + public static ExecutorService getOrInitExecutors(String poolName, int poolSize) { + return executors.computeIfAbsent(poolName, key -> init(poolName, poolSize)); + } + + public static void releaseExecutors(String poolName) { + ExecutorService executorService = executors.remove(poolName); + if (executorService != null) { + executorService.shutdown(); + } + } +} diff --git a/milky-concurrent/milky-concurrent-future/src/main/java/cn/sliew/milky/concurrent/threadpool/DaemonThreadFactory.java b/milky-concurrent/milky-concurrent-future/src/main/java/cn/sliew/milky/concurrent/threadpool/DaemonThreadFactory.java deleted file mode 100644 index 92cc669c..00000000 --- a/milky-concurrent/milky-concurrent-future/src/main/java/cn/sliew/milky/concurrent/threadpool/DaemonThreadFactory.java +++ /dev/null @@ -1,43 +0,0 @@ -package cn.sliew.milky.concurrent.threadpool; - -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.atomic.AtomicInteger; - -import static cn.sliew.milky.common.check.Ensures.checkNotNull; - -class DaemonThreadFactory implements ThreadFactory { - - private final int threadPriority = Thread.NORM_PRIORITY; - - private final String threadNamePrefix; - - private final boolean daemon; - - private final ThreadGroup threadGroup; - - private final AtomicInteger threadCount = new AtomicInteger(0); - - DaemonThreadFactory(String threadNamePrefix, boolean daemon, ThreadGroup threadGroup) { - this.threadNamePrefix = checkNotNull(threadNamePrefix); - this.daemon = daemon; - SecurityManager s = System.getSecurityManager(); - this.threadGroup = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); - } - - @Override - public Thread newThread(Runnable runnable) { - Thread thread = new Thread(threadGroup, runnable, nextThreadName()); - thread.setPriority(threadPriority); - thread.setDaemon(daemon); - return thread; - } - - /** - * Return the thread name to use for a newly created {@link Thread}. - *

The default implementation returns the specified thread name prefix - * with an increasing thread count appended: e.g. "SimpleAsyncTaskExecutor-0". - */ - protected String nextThreadName() { - return threadNamePrefix + this.threadCount.incrementAndGet(); - } -} diff --git a/milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/MilkyThreadPoolExecutor.java b/milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/MilkyThreadPoolExecutor.java index aeb1766a..b9814520 100644 --- a/milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/MilkyThreadPoolExecutor.java +++ b/milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/MilkyThreadPoolExecutor.java @@ -1,6 +1,7 @@ package cn.sliew.milky.concurrent.thread; import cn.sliew.milky.common.collect.ConcurrentReferenceHashMap; +import cn.sliew.milky.common.concurrent.DaemonThreadFactory; import cn.sliew.milky.common.concurrent.RunnableWrapper; import cn.sliew.milky.common.exception.Rethrower; import cn.sliew.milky.concurrent.thread.policy.AbortPolicyWithReport; diff --git a/milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/ThreadPoolExecutorBuilder.java b/milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/ThreadPoolExecutorBuilder.java index 1f5cd7c1..d49988e6 100644 --- a/milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/ThreadPoolExecutorBuilder.java +++ b/milky-concurrent/milky-concurrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/ThreadPoolExecutorBuilder.java @@ -1,5 +1,6 @@ package cn.sliew.milky.concurrent.thread; +import cn.sliew.milky.common.concurrent.DaemonThreadFactory; import cn.sliew.milky.common.unit.TimeValue; import cn.sliew.milky.common.unit.TimeValues; import cn.sliew.milky.concurrent.thread.metrics.ThreadPoolExecutorMetrics; diff --git a/pom.xml b/pom.xml index 8dcf6492..f32b5433 100644 --- a/pom.xml +++ b/pom.xml @@ -79,18 +79,52 @@ ${java.version} true 3.0.0-M5 + + 1.18.32 + 1.4.2.Final + 0.1.1 + 0.2.0 + 1.1.1 + 2.17.1 + 2.23.1 + 4.0.0 + 1.13.0 + 5.8.30 + 33.2.1-jre org.apiguardian apiguardian-api - 1.1.1 + ${apiguardian.version} + + com.fasterxml.jackson + jackson-bom + ${jackson.version} + pom + import + + + org.apache.logging.log4j + log4j-bom + ${log4j.version} + pom + import + + + io.micrometer + micrometer-bom + ${micrometer.version} + pom + import + + ${project.groupId} milky-common @@ -117,33 +151,37 @@ ${project.version} - - com.fasterxml.jackson - jackson-bom - 2.13.5 - pom - import + org.projectlombok + lombok + ${lombok.version} - org.apache.logging.log4j - log4j-bom - 2.17.1 - pom - import + org.mapstruct + mapstruct + ${org.mapstruct.version} - io.micrometer - micrometer-bom - 1.7.4 - pom - import + org.mapstruct.extensions.spring + mapstruct-spring-annotations + ${org.mapstruct.extensions.spring.version} + + + + cn.hutool + hutool-all + ${hutool.version} + + + com.google.guava + guava + ${guava.version} com.lmax disruptor - 3.4.4 + ${disruptor.version} @@ -367,6 +405,23 @@ **/package-info.java + + + org.projectlombok + lombok + ${lombok.version} + + + org.mapstruct + mapstruct-processor + ${mapstruct.version} + + + org.projectlombok + lombok-mapstruct-binding + ${mapstruct.lombok.binding.version} + +