mirror of
https://gitee.com/jd-platform-opensource/asyncTool.git
synced 2024-12-31 15:35:34 +08:00
让同步执行结果有个返回值
This commit is contained in:
parent
8278337a4a
commit
b7c36196f4
@ -24,9 +24,9 @@ public class Async {
|
|||||||
new LinkedBlockingQueue<>(),
|
new LinkedBlockingQueue<>(),
|
||||||
(ThreadFactory) Thread::new);
|
(ThreadFactory) Thread::new);
|
||||||
|
|
||||||
public static void beginWork(long timeout, ThreadPoolExecutor pool, WorkerWrapper... workerWrapper) throws ExecutionException, InterruptedException {
|
public static boolean beginWork(long timeout, ThreadPoolExecutor pool, WorkerWrapper... workerWrapper) throws ExecutionException, InterruptedException {
|
||||||
if(workerWrapper == null || workerWrapper.length == 0) {
|
if(workerWrapper == null || workerWrapper.length == 0) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
List<WorkerWrapper> workerWrappers = Arrays.stream(workerWrapper).collect(Collectors.toList());
|
List<WorkerWrapper> workerWrappers = Arrays.stream(workerWrapper).collect(Collectors.toList());
|
||||||
|
|
||||||
@ -37,20 +37,22 @@ public class Async {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
CompletableFuture.allOf(futures).get(timeout, TimeUnit.MILLISECONDS);
|
CompletableFuture.allOf(futures).get(timeout, TimeUnit.MILLISECONDS);
|
||||||
|
return true;
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
Set<WorkerWrapper> set = new HashSet<>();
|
Set<WorkerWrapper> set = new HashSet<>();
|
||||||
totalWorkers(workerWrappers, set);
|
totalWorkers(workerWrappers, set);
|
||||||
for (WorkerWrapper wrapper : set) {
|
for (WorkerWrapper wrapper : set) {
|
||||||
wrapper.stopNow();
|
wrapper.stopNow();
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 同步阻塞,直到所有都完成,或失败
|
* 同步阻塞,直到所有都完成,或失败
|
||||||
*/
|
*/
|
||||||
public static void beginWork(long timeout, WorkerWrapper... workerWrapper) throws ExecutionException, InterruptedException {
|
public static boolean beginWork(long timeout, WorkerWrapper... workerWrapper) throws ExecutionException, InterruptedException {
|
||||||
beginWork(timeout, COMMON_POOL, workerWrapper);
|
return beginWork(timeout, COMMON_POOL, workerWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -63,8 +65,12 @@ public class Async {
|
|||||||
IGroupCallback finalGroupCallback = groupCallback;
|
IGroupCallback finalGroupCallback = groupCallback;
|
||||||
CompletableFuture.runAsync(() -> {
|
CompletableFuture.runAsync(() -> {
|
||||||
try {
|
try {
|
||||||
beginWork(timeout, COMMON_POOL, workerWrapper);
|
boolean success = beginWork(timeout, COMMON_POOL, workerWrapper);
|
||||||
finalGroupCallback.success(Arrays.asList(workerWrapper));
|
if (success) {
|
||||||
|
finalGroupCallback.success(Arrays.asList(workerWrapper));
|
||||||
|
} else {
|
||||||
|
finalGroupCallback.failure(Arrays.asList(workerWrapper), new TimeoutException());
|
||||||
|
}
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
finalGroupCallback.failure(Arrays.asList(workerWrapper), e);
|
finalGroupCallback.failure(Arrays.asList(workerWrapper), e);
|
||||||
|
Loading…
Reference in New Issue
Block a user