Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
@@ -81,36 +81,40 @@ public class AsyncLatchUtils {
|
||||
|
||||
System.out.println("主流程开始,准备分发异步任务...");
|
||||
|
||||
System.out.println("主线程id:" + Thread.currentThread().getId());
|
||||
// 2. 提交多个异步任务
|
||||
// 任务一:获取用户信息
|
||||
AsyncLatchUtils.submitTask(executorService, () -> {
|
||||
try {
|
||||
try {
|
||||
System.out.println("任务一子线程id:" + Thread.currentThread().getId());
|
||||
System.out.println("开始获取用户信息...");
|
||||
Thread.sleep(1000); // 模拟耗时
|
||||
System.out.println("获取用户信息成功!");
|
||||
} catch (InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
|
||||
// 任务二:获取订单信息
|
||||
AsyncLatchUtils.submitTask(executorService, () -> {
|
||||
try {
|
||||
try {
|
||||
System.out.println("任务二子线程id:" + Thread.currentThread().getId());
|
||||
System.out.println("开始获取订单信息...");
|
||||
Thread.sleep(1500); // 模拟耗时
|
||||
System.out.println("获取订单信息成功!");
|
||||
} catch (InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
|
||||
// 任务三:获取商品信息
|
||||
AsyncLatchUtils.submitTask(executorService, () -> {
|
||||
try {
|
||||
try {
|
||||
System.out.println("任务三子线程id:" + Thread.currentThread().getId());
|
||||
System.out.println("开始获取商品信息...");
|
||||
Thread.sleep(500); // 模拟耗时
|
||||
System.out.println("获取商品信息成功!");
|
||||
} catch (InterruptedException e) {
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
});
|
||||
@@ -118,12 +122,12 @@ public class AsyncLatchUtils {
|
||||
System.out.println("所有异步任务已提交,主线程开始等待...");
|
||||
|
||||
// 3. 等待所有任务完成,最长等待5秒
|
||||
boolean allTasksCompleted = AsyncLatchUtils.waitFor(5, TimeUnit.SECONDS);
|
||||
boolean allTasksCompleted = AsyncLatchUtils.waitFor(5, TimeUnit.SECONDS);
|
||||
|
||||
// 4. 根据等待结果继续主流程
|
||||
if (allTasksCompleted) {
|
||||
System.out.println("所有异步任务执行成功,主流程继续...");
|
||||
} else {
|
||||
} else {
|
||||
System.err.println("有任务执行超时,主流程中断!");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user