add AsyncUtil

This commit is contained in:
Looly 2021-11-10 23:12:53 +08:00
parent 2a7563a609
commit 8fda1b9ac5
6 changed files with 76 additions and 23 deletions

View File

@ -6,6 +6,7 @@
# 5.7.17 (2021-11-10)
### 🐣新特性
* 【core 】 增加AsyncUtilpr#457@Gitee
### 🐞Bug修复
* 【core 】 修复FileResource构造fileName参数无效问题issue#1942@Github

View File

@ -1,22 +1,17 @@
package cn.hutool.core.thread;
import cn.hutool.core.exceptions.ExceptionUtil;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/**
* CompletableFuture工具类叫CompletableFutureUtil太长
* {@link CompletableFuture}异步工具类<br>
* {@link CompletableFuture} Future 的改进可以通过传入回调对象在任务完成后调用之
*
* @author <achao1441470436@gmail.com>
* @since 2021/11/10 0010 20:55
* @since 5.7.17
*/
public class SyncUtil {
private SyncUtil() {
/* Do not new me! */
}
public class AsyncUtil {
/**
* 等待所有任务执行完毕包裹了异常
@ -24,11 +19,25 @@ public class SyncUtil {
* @param tasks 并行任务
* @throws UndeclaredThrowableException 未受检异常
*/
public static void wait(CompletableFuture<?>... tasks) {
public static void waitAll(CompletableFuture<?>... tasks) {
try {
CompletableFuture.allOf(tasks).get();
} catch (InterruptedException | ExecutionException e) {
ExceptionUtil.wrapAndThrow(e);
throw new ThreadException(e);
}
}
/**
* 等待任意一个任务执行完毕包裹了异常
*
* @param tasks 并行任务
* @throws UndeclaredThrowableException 未受检异常
*/
public static void waitAny(CompletableFuture<?>... tasks) {
try {
CompletableFuture.anyOf(tasks).get();
} catch (InterruptedException | ExecutionException e) {
throw new ThreadException(e);
}
}
@ -41,13 +50,11 @@ public class SyncUtil {
* @throws RuntimeException 未受检异常
*/
public static <T> T get(CompletableFuture<T> task) {
RuntimeException exception;
try {
return task.get();
} catch (InterruptedException | ExecutionException e) {
exception = ExceptionUtil.wrapRuntime(e);
throw new ThreadException(e);
}
throw exception;
}
}

View File

@ -0,0 +1,38 @@
package cn.hutool.core.thread;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
/**
* 工具类异常
*
* @author looly
* @since 5.7.17
*/
public class ThreadException extends RuntimeException {
private static final long serialVersionUID = 5253124428623713216L;
public ThreadException(Throwable e) {
super(ExceptionUtil.getMessage(e), e);
}
public ThreadException(String message) {
super(message);
}
public ThreadException(String messageTemplate, Object... params) {
super(StrUtil.format(messageTemplate, params));
}
public ThreadException(String message, Throwable throwable) {
super(message, throwable);
}
public ThreadException(String message, Throwable throwable, boolean enableSuppression, boolean writableStackTrace) {
super(message, throwable, enableSuppression, writableStackTrace);
}
public ThreadException(Throwable throwable, String messageTemplate, Object... params) {
super(StrUtil.format(messageTemplate, params), throwable);
}
}

View File

@ -12,7 +12,6 @@ import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
/**
@ -1362,12 +1361,12 @@ public class NumberUtil {
throw new UtilException("Size is larger than range between begin and end!");
}
Set<Integer> set = new HashSet<>(Math.max((int) (size / .75f) + 1, 16));
Set<Integer> set = new HashSet<>(size, 1);
while (set.size() < size) {
set.add(begin + RandomUtil.randomInt(end - begin));
}
return set.toArray(new Integer[size]);
return set.toArray(new Integer[0]);
}
// ------------------------------------------------------------------------------------------- range

View File

@ -1,6 +1,7 @@
package cn.hutool.core.thread;
import cn.hutool.core.lang.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.concurrent.CompletableFuture;
@ -12,25 +13,26 @@ import java.util.concurrent.TimeUnit;
* @author <achao1441470436@gmail.com>
* @since 2021/11/10 0010 21:15
*/
public class SyncUtilTest {
public class AsyncUtilTest {
@Test
@Ignore
public void waitAndGetTest() {
CompletableFuture<String> hutool = CompletableFuture.supplyAsync(() -> {
ThreadUtil.sleep(3, TimeUnit.SECONDS);
ThreadUtil.sleep(1, TimeUnit.SECONDS);
return "hutool";
});
CompletableFuture<String> sweater = CompletableFuture.supplyAsync(() -> {
ThreadUtil.sleep(4, TimeUnit.SECONDS);
ThreadUtil.sleep(2, TimeUnit.SECONDS);
return "卫衣";
});
CompletableFuture<String> warm = CompletableFuture.supplyAsync(() -> {
ThreadUtil.sleep(5, TimeUnit.SECONDS);
ThreadUtil.sleep(3, TimeUnit.SECONDS);
return "真暖和";
});
// 等待完成
SyncUtil.wait(hutool, sweater, warm);
AsyncUtil.waitAll(hutool, sweater, warm);
// 获取结果
Assert.isTrue("hutool卫衣真暖和".equals(SyncUtil.get(hutool) + SyncUtil.get(sweater) + SyncUtil.get(warm)));
Assert.isTrue("hutool卫衣真暖和".equals(AsyncUtil.get(hutool) + AsyncUtil.get(sweater) + AsyncUtil.get(warm)));
}
}

View File

@ -400,4 +400,10 @@ public class NumberUtilTest {
final String s = new BigDecimal(num).toPlainString();
Assert.assertEquals("5344342.34", s);
}
@Test
public void generateBySetTest(){
final Integer[] integers = NumberUtil.generateBySet(10, 100, 5);
Assert.assertEquals(5, integers.length);
}
}