-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wangqi
committed
Nov 22, 2024
1 parent
6fa75cb
commit 8517c2d
Showing
9 changed files
with
145 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
milky-common/src/main/java/cn/sliew/milky/common/concurrent/AbstractLoopRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
milky-common/src/main/java/cn/sliew/milky/common/concurrent/LoopRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
milky-common/src/main/java/cn/sliew/milky/common/concurrent/ThreadPoolUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
43 changes: 0 additions & 43 deletions
43
...urrent-future/src/main/java/cn/sliew/milky/concurrent/threadpool/DaemonThreadFactory.java
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...urrent-thread/src/main/java/cn/sliew/milky/concurrent/thread/MilkyThreadPoolExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...rent-thread/src/main/java/cn/sliew/milky/concurrent/thread/ThreadPoolExecutorBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters