Skip to content

Commit

Permalink
feature: update thread util
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqi committed Nov 22, 2024
1 parent 6fa75cb commit 8517c2d
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 74 deletions.
11 changes: 5 additions & 6 deletions milky-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<artifactId>disruptor</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand All @@ -44,12 +49,6 @@
<artifactId>jackson-dataformat-properties</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.sliew</groupId>
<artifactId>milky-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,15 +19,15 @@ public DaemonThreadFactory() {
this(null);
}

DaemonThreadFactory(String threadNamePrefix) {
public DaemonThreadFactory(String threadNamePrefix) {
this(threadNamePrefix, true);
}

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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cn.sliew.milky.common.concurrent;

import java.util.concurrent.*;

public enum ThreadPoolUtil {
;

private static ConcurrentMap<String, ExecutorService> executors = new ConcurrentHashMap<>();

private static ExecutorService init(String poolName, int poolSize) {
return new ThreadPoolExecutor(poolSize, poolSize,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
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();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
91 changes: 73 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,52 @@
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.test.skip>true</maven.test.skip>
<surefire.version>3.0.0-M5</surefire.version>

<lombok.version>1.18.32</lombok.version>
<mapstruct.version>1.4.2.Final</mapstruct.version>
<mapstruct-spring.version>0.1.1</mapstruct-spring.version>
<mapstruct.lombok.binding.version>0.2.0</mapstruct.lombok.binding.version>
<apiguardian.version>1.1.1</apiguardian.version>
<jackson.version>2.17.1</jackson.version>
<log4j.version>2.23.1</log4j.version>
<disruptor.version>4.0.0</disruptor.version>
<micrometer.version>1.13.0</micrometer.version>
<hutool.version>5.8.30</hutool.version>
<guava.version>33.2.1-jre</guava.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apiguardian</groupId>
<artifactId>apiguardian-api</artifactId>
<version>1.1.1</version>
<version>${apiguardian.version}</version>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>${jackson.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>${log4j.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-bom</artifactId>
<version>${micrometer.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>milky-common</artifactId>
Expand All @@ -117,33 +151,37 @@
<version>${project.version}</version>
</dependency>


<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.13.5</version>
<type>pom</type>
<scope>import</scope>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.17.1</version>
<type>pom</type>
<scope>import</scope>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-bom</artifactId>
<version>1.7.4</version>
<type>pom</type>
<scope>import</scope>
<groupId>org.mapstruct.extensions.spring</groupId>
<artifactId>mapstruct-spring-annotations</artifactId>
<version>${org.mapstruct.extensions.spring.version}</version>
</dependency>

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>

<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.4</version>
<version>${disruptor.version}</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -367,6 +405,23 @@
<excludes>
<exclude>**/package-info.java</exclude>
</excludes>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${mapstruct.lombok.binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
Expand Down

0 comments on commit 8517c2d

Please sign in to comment.