Skip to content

Commit

Permalink
Merge pull request #39 from pugwoo/develop-daily
Browse files Browse the repository at this point in the history
release 1.3.2
  • Loading branch information
pugwoo authored May 24, 2024
2 parents c8448ac + 094a2d7 commit de835f7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2024年5月24日
v1.3.2 - [optimize] 优化EasyRunTask创建线程池的性能,减少重复创建线程池的次数
- [enhance] 优化MDCUtils工具对mdc为null时的处理

2024年5月20日
v1.3.1 - [fix] 修复EasyRunTask中创建线程池没有shutdown而导致内存泄漏的问题

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<dependency>
<groupId>com.pugwoo</groupId>
<artifactId>woo-utils</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>
</dependency>
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>com.pugwoo</groupId>
<artifactId>woo-utils</artifactId>
<packaging>jar</packaging>
<version>1.3.1</version>
<version>1.3.2</version>

<name>woo-utils</name>
<description>the common utils</description>
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/com/pugwoo/wooutils/log/MDCUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,38 @@ public class MDCUtils {
public static Runnable withMdc(Runnable runnable) {
Map<String, String> mdc = MDC.getCopyOfContextMap();
return () -> {
MDC.setContextMap(mdc);
if (mdc != null) {
try {
MDC.setContextMap(mdc);
} catch (Throwable ignored) {
}
}
runnable.run();
};
}

public static <U> Supplier<U> withMdcSupplier(Supplier<U> supplier) {
Map<String, String> mdc = MDC.getCopyOfContextMap();
return () -> {
MDC.setContextMap(mdc);
if (mdc != null) {
try {
MDC.setContextMap(mdc);
} catch (Throwable ignored) {
}
}
return supplier.get();
};
}

public static <U> Callable<U> withMdcCallable(Callable<U> callable) {
Map<String, String> mdc = MDC.getCopyOfContextMap();
return () -> {
MDC.setContextMap(mdc);
if (mdc != null) {
try {
MDC.setContextMap(mdc);
} catch (Throwable ignored) {
}
}
return callable.call();
};
}
Expand Down
110 changes: 57 additions & 53 deletions src/main/java/com/pugwoo/wooutils/task/EasyRunTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,69 +116,73 @@ private synchronized TaskResult run(boolean reset) {
status = TaskStatusEnum.RUNNING;

new Thread(MDCUtils.withMdc(() -> {
while(true) {
synchronized (that) { // 请求停止
if(status == TaskStatusEnum.STOPPING) {
status = TaskStatusEnum.STOPPED;
endTime = new Date();
return;
ThreadPoolExecutor executeThem = concurrentNum == 1 ? null :
ThreadPoolUtils.createThreadPool(concurrentNum, 0,
concurrentNum, "easyRunTask", true);

try {
while(true) {
synchronized (that) { // 请求停止
if(status == TaskStatusEnum.STOPPING) {
status = TaskStatusEnum.STOPPED;
endTime = new Date();
return;
}
}
}

int restCount = getRestCount();
if(getRestCount() <= 0) {
synchronized (that) { // 结束任务
status = TaskStatusEnum.FINISHED;
endTime = new Date();
int restCount = getRestCount();
if(getRestCount() <= 0) {
synchronized (that) { // 结束任务
status = TaskStatusEnum.FINISHED;
endTime = new Date();
}
return;
}
return;
}

// 多线程执行任务,实际上,是一批一批地去执行,这样才能中途控制其停下
int nThreads = Math.min(restCount, concurrentNum);
if (nThreads < 1) {
nThreads = 1;
}
if (nThreads == 1) { // 如果是1个线程,则直接执行,不用多线程,节省资源
try {
TaskResult result = task.runStep();
if(result == null || !result.isSuccess()) {
if (executeThem == null) { // 如果是1个线程,则直接执行,不用多线程,节省资源
try {
TaskResult result = task.runStep();
if(result == null || !result.isSuccess()) {
fail.incrementAndGet();
} else {
success.incrementAndGet();
}
} catch (Throwable e) {
exceptions.add(e);
fail.incrementAndGet();
} else {
success.incrementAndGet();
} finally {
processed.incrementAndGet();
}
} catch (Throwable e) {
exceptions.add(e);
fail.incrementAndGet();
} finally {
processed.incrementAndGet();
}
} else {
ThreadPoolExecutor executeThem = ThreadPoolUtils.createThreadPool(nThreads, 0,
nThreads, "easyRunTask");
List<Future<String>> futures = new ArrayList<>();
for(int i = 0; i < nThreads; i++) {
futures.add(executeThem.submit(() -> {
try {
TaskResult result = task.runStep();
if(result == null || !result.isSuccess()) {
} else {
// 多线程执行任务,实际上,是一批一批地去执行,这样才能中途控制其停下
int nThreads = Math.min(restCount, concurrentNum);
List<Future<String>> futures = new ArrayList<>();
for(int i = 0; i < nThreads; i++) {
futures.add(executeThem.submit(() -> {
try {
TaskResult result = task.runStep();
if(result == null || !result.isSuccess()) {
fail.incrementAndGet();
} else {
success.incrementAndGet();
}
} catch (Throwable e) {
exceptions.add(e);
fail.incrementAndGet();
} else {
success.incrementAndGet();
} finally {
processed.incrementAndGet();
}
} catch (Throwable e) {
exceptions.add(e);
fail.incrementAndGet();
} finally {
processed.incrementAndGet();
}
}, ""));
}, ""));
}
ThreadPoolUtils.waitAllFuturesDone(futures);
}
ThreadPoolUtils.waitAllFuturesDone(futures);
executeThem.shutdown();
}

total.set(processed.get() + getRestCount());
total.set(processed.get() + getRestCount());
}
} finally {
if (executeThem != null) {
executeThem.shutdown(); // 线程池一定要释放掉
}
}
}), "EasyRunTaskExecute").start();

Expand Down

0 comments on commit de835f7

Please sign in to comment.