Skip to content

Commit ec6f8b3

Browse files
authored
Merge pull request #25 from lunasaw/2.5.7
2.5.7 线程池优化
2 parents 3cbba2a + be865cc commit ec6f8b3

File tree

3 files changed

+96
-19
lines changed

3 files changed

+96
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<groupId>io.github.lunasaw</groupId>
88
<artifactId>luna-common</artifactId>
99
<name>luna-common</name>
10-
<version>2.5.6</version>
10+
<version>2.5.7</version>
1111
<description>common is project which contains common utils</description>
1212
<url>https://github.com/lunasaw/luna-common</url>
1313

src/main/java/com/luna/common/thread/AsyncEngineUtils.java

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.luna.common.thread;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54
import java.util.concurrent.*;
65

76
import org.apache.commons.collections4.CollectionUtils;
8-
import org.checkerframework.checker.nullness.qual.Nullable;
97
import org.slf4j.Logger;
108
import org.slf4j.LoggerFactory;
119

@@ -16,23 +14,56 @@
1614
*/
1715
public class AsyncEngineUtils {
1816

19-
private static final Logger log = LoggerFactory.getLogger(AsyncEngineUtils.class);
17+
private static final Logger log = LoggerFactory.getLogger(AsyncEngineUtils.class);
2018

21-
private static final int CORE_POOL_SIZE = 100;
19+
private static final int CORE_POOL_SIZE = 200;
2220

23-
private static final int MAX_POOL_SIZE = 200;
21+
private static final int MAX_POOL_SIZE = 200;
2422

25-
private static final ExecutorService executor;
23+
private static final int KEEP_ALIVE_TIME = 60 * 5;
24+
25+
private static final int QUEUE_CAPACITY = 1000;
26+
27+
private static final long TIME_OUT = 300;
28+
29+
private static final int MONITOR_PERIOD = 5; // 监控时间间隔,单位:s
30+
31+
private static final ExecutorService EXECUTOR;
32+
33+
private static final Runnable MONITOR_TASK = new Runnable() {
34+
@Override
35+
public void run() {
36+
try {
37+
ThreadPoolExecutor threadPool = (ThreadPoolExecutor)EXECUTOR;
38+
int activeCount = threadPool.getActiveCount(); // 正在执行的任务数
39+
long completedTaskCount = threadPool.getCompletedTaskCount(); // 已完成任务数
40+
long totalTaskCount = threadPool.getTaskCount(); // 总任务数
41+
int queueSize = threadPool.getQueue().size();
42+
int coreSize = threadPool.getCorePoolSize();
43+
44+
log.info(
45+
"total_task:{}, active_thread:{}, queue_size:{}, completed_thread:{}, coreSize:{}",
46+
totalTaskCount, activeCount, queueSize, completedTaskCount, coreSize);
47+
48+
} catch (Exception e) {
49+
log.error("[SYSTEM-SafeGuard]Monitor thread run fail", e);
50+
}
51+
}
52+
};
2653

2754
static {
28-
executor = new ThreadPoolExecutor(
55+
EXECUTOR = new ThreadPoolExecutor(
2956
CORE_POOL_SIZE,
3057
MAX_POOL_SIZE,
31-
60 * 5L,
58+
KEEP_ALIVE_TIME,
3259
TimeUnit.SECONDS,
33-
new SynchronousQueue<>(),
60+
new LinkedBlockingDeque<>(QUEUE_CAPACITY),
3461
Executors.defaultThreadFactory(),
3562
new ThreadPoolExecutor.CallerRunsPolicy());
63+
64+
ScheduledExecutorService monitor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("AsyncEngine-Monitor", true));
65+
monitor.scheduleAtFixedRate(MONITOR_TASK, MONITOR_PERIOD, MONITOR_PERIOD, TimeUnit.SECONDS);
66+
3667
}
3768

3869
/**
@@ -41,11 +72,12 @@ public class AsyncEngineUtils {
4172
* @param tasks 任务
4273
* @return T 任务返回值
4374
*/
75+
@SafeVarargs
4476
public static <T> List<T> concurrentExecute(Callable<T>... tasks) {
4577
if (tasks == null || tasks.length == 0) {
4678
return Lists.newArrayList();
4779
}
48-
return concurrentExecute(-1, null, tasks);
80+
return concurrentExecute(-1, null, Lists.newArrayList(tasks));
4981
}
5082

5183
/**
@@ -55,12 +87,11 @@ public static <T> List<T> concurrentExecute(Callable<T>... tasks) {
5587
* @return T 任务返回值
5688
*/
5789
public static <T> List<T> concurrentExecute(List<Callable<T>> tasks) {
58-
5990
if (CollectionUtils.isEmpty(tasks)) {
6091
return Lists.newArrayList();
6192
}
6293

63-
return concurrentExecute(tasks.toArray(new Callable[tasks.size()]));
94+
return concurrentExecute(-1, null, tasks);
6495
}
6596

6697
/**
@@ -71,23 +102,25 @@ public static <T> List<T> concurrentExecute(List<Callable<T>> tasks) {
71102
* @param tasks 任务
72103
* @return T 任务返回值
73104
*/
74-
public static <T> List<T> concurrentExecute(long timeout, TimeUnit unit, Callable<T>... tasks) {
75-
if (tasks == null || tasks.length == 0) {
105+
public static <T> List<T> concurrentExecute(long timeout, TimeUnit unit, List<Callable<T>> tasks) {
106+
if (CollectionUtils.isEmpty(tasks)) {
76107
return Lists.newArrayList();
77108
}
78109

79110
List<T> result = Lists.newArrayList();
80111
try {
81-
List<Future<T>> futures = timeout > 0 ? executor.invokeAll(Lists.newArrayList(tasks), timeout, unit)
82-
: executor.invokeAll(Lists.newArrayList(tasks));
112+
List<Future<T>> futures = timeout > 0 ? EXECUTOR.invokeAll(tasks, timeout, unit)
113+
: EXECUTOR.invokeAll(tasks);
83114
for (Future<T> future : futures) {
84115
T t = null;
85116
try {
86-
t = future.get();
117+
t = future.get(TIME_OUT, TimeUnit.MILLISECONDS);
87118
} catch (CancellationException e) {
88119
if (timeout > 0) {
89120
log.error("concurrentExecute some task timeout!");
90121
}
122+
} catch (TimeoutException tt) {
123+
log.error("future.get() TimeoutException ", tt);
91124
} catch (Throwable tt) {
92125
log.error("future.get() Exception ", tt);
93126
}
@@ -108,7 +141,7 @@ public static void execute(Runnable task) {
108141
if (task == null) {
109142
return;
110143
}
111-
executor.submit(task);
144+
EXECUTOR.submit(task);
112145
}
113146

114147
public static void main(String[] args) {
@@ -122,4 +155,10 @@ public static void main(String[] args) {
122155
List<Void> voids = concurrentExecute(list);
123156
System.out.println(voids);
124157
}
158+
159+
public static void destroy() {
160+
log.warn("start to stop thread pool");
161+
EXECUTOR.shutdown();
162+
log.warn("finish to stop thread pool");
163+
}
125164
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.luna.common.thread;
2+
3+
import java.util.concurrent.ThreadFactory;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
/**
7+
* thread 命名
8+
*
9+
* @author luna
10+
**/
11+
public class NamedThreadFactory implements ThreadFactory {
12+
private static final AtomicInteger poolNumber = new AtomicInteger(1);
13+
14+
private final AtomicInteger threadNumber = new AtomicInteger(1);
15+
private final ThreadGroup group;
16+
private final String namePrefix;
17+
private final boolean isDaemon;
18+
19+
public NamedThreadFactory(String name) {
20+
this(name, false);
21+
}
22+
23+
public NamedThreadFactory(String prefix, boolean daemon) {
24+
SecurityManager s = System.getSecurityManager();
25+
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
26+
namePrefix = prefix + "-" + poolNumber.getAndIncrement() + "-thread-";
27+
isDaemon = daemon;
28+
}
29+
30+
public Thread newThread(Runnable runnable) {
31+
Thread t = new Thread(group, runnable, namePrefix + threadNumber.getAndIncrement(), 0);
32+
// Default value is parent thread's
33+
t.setContextClassLoader(NamedThreadFactory.class.getClassLoader());
34+
t.setPriority(Thread.MAX_PRIORITY);
35+
t.setDaemon(isDaemon);
36+
return t;
37+
}
38+
}

0 commit comments

Comments
 (0)