Merge pull request !5 from tangzekk/V1.4
This commit is contained in:
tianyaleixiaowu 2021-04-14 09:39:50 +08:00 committed by Gitee
commit f323f37ded

View File

@ -15,6 +15,9 @@ import java.util.stream.Collectors;
* @version 1.0 * @version 1.0
*/ */
public class Async { public class Async {
/**
* 默认线程池
*/
private static final ThreadPoolExecutor COMMON_POOL = private static final ThreadPoolExecutor COMMON_POOL =
new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2, 1024, new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2, 1024,
15L, TimeUnit.SECONDS, 15L, TimeUnit.SECONDS,
@ -72,10 +75,14 @@ public class Async {
return beginWork(timeout, COMMON_POOL, workerWrapper); return beginWork(timeout, COMMON_POOL, workerWrapper);
} }
public static void beginWorkAsync(long timeout, IGroupCallback groupCallback, WorkerWrapper... workerWrapper) {
beginWorkAsync(timeout, COMMON_POOL, groupCallback, workerWrapper);
}
/** /**
* 异步执行,直到所有都完成,或失败后发起回调 * 异步执行,直到所有都完成,或失败后发起回调
*/ */
public static void beginWorkAsync(long timeout, IGroupCallback groupCallback, WorkerWrapper... workerWrapper) { public static void beginWorkAsync(long timeout, ExecutorService executorService, IGroupCallback groupCallback, WorkerWrapper... workerWrapper) {
if (groupCallback == null) { if (groupCallback == null) {
groupCallback = new DefaultGroupCallback(); groupCallback = new DefaultGroupCallback();
} }
@ -83,7 +90,7 @@ public class Async {
if (executorService != null) { if (executorService != null) {
executorService.submit(() -> { executorService.submit(() -> {
try { try {
boolean success = beginWork(timeout, COMMON_POOL, workerWrapper); boolean success = beginWork(timeout, executorService, workerWrapper);
if (success) { if (success) {
finalGroupCallback.success(Arrays.asList(workerWrapper)); finalGroupCallback.success(Arrays.asList(workerWrapper));
} else { } else {