mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
!1022 异步工具类添加一次性获取所有结果方法
Merge pull request !1022 from 孔皮皮/v6-dev-async
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package org.dromara.hutool.core.thread;
|
||||
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -35,4 +39,89 @@ public class AsyncUtilTest {
|
||||
// 获取结果
|
||||
Assertions.assertEquals("hutool卫衣真暖和", AsyncUtil.get(hutool) + AsyncUtil.get(sweater) + AsyncUtil.get(warm));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void allGetTest() {
|
||||
final CompletableFuture<String> hutool = CompletableFuture.supplyAsync(() -> {
|
||||
ThreadUtil.sleep(1, TimeUnit.SECONDS);
|
||||
return "hutool";
|
||||
});
|
||||
final CompletableFuture<String> sweater = CompletableFuture.supplyAsync(() -> {
|
||||
ThreadUtil.sleep(2, TimeUnit.SECONDS);
|
||||
return "卫衣";
|
||||
});
|
||||
final CompletableFuture<String> warm = CompletableFuture.supplyAsync(() -> {
|
||||
ThreadUtil.sleep(3, TimeUnit.SECONDS);
|
||||
return "真暖和";
|
||||
});
|
||||
// 等待完成
|
||||
List<String> list = AsyncUtil.allOfGet(ListUtil.of(hutool, sweater, warm));
|
||||
// 获取结果
|
||||
Assertions.assertEquals(Arrays.asList("hutool", "卫衣", "真暖和"), list);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void allGetTestException() {
|
||||
final CompletableFuture<String> hutool = CompletableFuture.supplyAsync(() -> {
|
||||
ThreadUtil.sleep(1, TimeUnit.SECONDS);
|
||||
return "hutool";
|
||||
});
|
||||
final CompletableFuture<String> sweater = CompletableFuture.supplyAsync(() -> {
|
||||
int a = 1 / 0;
|
||||
ThreadUtil.sleep(2, TimeUnit.SECONDS);
|
||||
return "卫衣";
|
||||
});
|
||||
final CompletableFuture<String> warm = CompletableFuture.supplyAsync(() -> {
|
||||
ThreadUtil.sleep(3, TimeUnit.SECONDS);
|
||||
return "真暖和";
|
||||
});
|
||||
// 等待完成
|
||||
List<String> list = AsyncUtil.allOfGet(ListUtil.of(hutool, sweater, warm), (e) -> "出错了");
|
||||
// 获取结果
|
||||
Assertions.assertEquals(Arrays.asList("hutool", "卫衣", "真暖和"), list);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void parallelAllOfGetTest() {
|
||||
final CompletableFuture<String> hutool = CompletableFuture.supplyAsync(() -> {
|
||||
ThreadUtil.sleep(1, TimeUnit.SECONDS);
|
||||
return "hutool";
|
||||
});
|
||||
final CompletableFuture<String> sweater = CompletableFuture.supplyAsync(() -> {
|
||||
ThreadUtil.sleep(2, TimeUnit.SECONDS);
|
||||
return "卫衣";
|
||||
});
|
||||
final CompletableFuture<String> warm = CompletableFuture.supplyAsync(() -> {
|
||||
ThreadUtil.sleep(3, TimeUnit.SECONDS);
|
||||
return "真暖和";
|
||||
});
|
||||
// 等待完成
|
||||
List<String> list = AsyncUtil.parallelAllOfGet(ListUtil.of(hutool, sweater, warm));
|
||||
// 获取结果
|
||||
Assertions.assertEquals(Arrays.asList("hutool", "卫衣", "真暖和"), list);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void parallelAllOfGetTestException() {
|
||||
final CompletableFuture<String> hutool = CompletableFuture.supplyAsync(() -> {
|
||||
ThreadUtil.sleep(1, TimeUnit.SECONDS);
|
||||
return "hutool";
|
||||
});
|
||||
final CompletableFuture<String> sweater = CompletableFuture.supplyAsync(() -> {
|
||||
int a = 1 / 0;
|
||||
ThreadUtil.sleep(2, TimeUnit.SECONDS);
|
||||
return "卫衣";
|
||||
});
|
||||
final CompletableFuture<String> warm = CompletableFuture.supplyAsync(() -> {
|
||||
ThreadUtil.sleep(3, TimeUnit.SECONDS);
|
||||
return "真暖和";
|
||||
});
|
||||
// 等待完成
|
||||
List<String> list = AsyncUtil.parallelAllOfGet(ListUtil.of(hutool, sweater, warm), (e) -> "出错了");
|
||||
Assertions.assertEquals(Arrays.asList("hutool", "出错了", "真暖和"), list);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user