mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add AsyncUtil
This commit is contained in:
parent
2a7563a609
commit
8fda1b9ac5
@ -6,6 +6,7 @@
|
|||||||
# 5.7.17 (2021-11-10)
|
# 5.7.17 (2021-11-10)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
|
* 【core 】 增加AsyncUtil(pr#457@Gitee)
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github)
|
* 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github)
|
||||||
|
|
||||||
|
@ -1,22 +1,17 @@
|
|||||||
package cn.hutool.core.thread;
|
package cn.hutool.core.thread;
|
||||||
|
|
||||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
||||||
|
|
||||||
import java.lang.reflect.UndeclaredThrowableException;
|
import java.lang.reflect.UndeclaredThrowableException;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CompletableFuture工具类,叫CompletableFutureUtil太长
|
* {@link CompletableFuture}异步工具类<br>
|
||||||
|
* {@link CompletableFuture} 是 Future 的改进,可以通过传入回调对象,在任务完成后调用之
|
||||||
*
|
*
|
||||||
* @author <achao1441470436@gmail.com>
|
* @author <achao1441470436@gmail.com>
|
||||||
* @since 2021/11/10 0010 20:55
|
* @since 5.7.17
|
||||||
*/
|
*/
|
||||||
public class SyncUtil {
|
public class AsyncUtil {
|
||||||
|
|
||||||
private SyncUtil() {
|
|
||||||
/* Do not new me! */
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 等待所有任务执行完毕,包裹了异常
|
* 等待所有任务执行完毕,包裹了异常
|
||||||
@ -24,11 +19,25 @@ public class SyncUtil {
|
|||||||
* @param tasks 并行任务
|
* @param tasks 并行任务
|
||||||
* @throws UndeclaredThrowableException 未受检异常
|
* @throws UndeclaredThrowableException 未受检异常
|
||||||
*/
|
*/
|
||||||
public static void wait(CompletableFuture<?>... tasks) {
|
public static void waitAll(CompletableFuture<?>... tasks) {
|
||||||
try {
|
try {
|
||||||
CompletableFuture.allOf(tasks).get();
|
CompletableFuture.allOf(tasks).get();
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
} 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 未受检异常
|
* @throws RuntimeException 未受检异常
|
||||||
*/
|
*/
|
||||||
public static <T> T get(CompletableFuture<T> task) {
|
public static <T> T get(CompletableFuture<T> task) {
|
||||||
RuntimeException exception;
|
|
||||||
try {
|
try {
|
||||||
return task.get();
|
return task.get();
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
exception = ExceptionUtil.wrapRuntime(e);
|
throw new ThreadException(e);
|
||||||
}
|
}
|
||||||
throw exception;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -12,7 +12,6 @@ import java.text.NumberFormat;
|
|||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1362,12 +1361,12 @@ public class NumberUtil {
|
|||||||
throw new UtilException("Size is larger than range between begin and end!");
|
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) {
|
while (set.size() < size) {
|
||||||
set.add(begin + RandomUtil.randomInt(end - begin));
|
set.add(begin + RandomUtil.randomInt(end - begin));
|
||||||
}
|
}
|
||||||
|
|
||||||
return set.toArray(new Integer[size]);
|
return set.toArray(new Integer[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------- range
|
// ------------------------------------------------------------------------------------------- range
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.hutool.core.thread;
|
package cn.hutool.core.thread;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@ -12,25 +13,26 @@ import java.util.concurrent.TimeUnit;
|
|||||||
* @author <achao1441470436@gmail.com>
|
* @author <achao1441470436@gmail.com>
|
||||||
* @since 2021/11/10 0010 21:15
|
* @since 2021/11/10 0010 21:15
|
||||||
*/
|
*/
|
||||||
public class SyncUtilTest {
|
public class AsyncUtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Ignore
|
||||||
public void waitAndGetTest() {
|
public void waitAndGetTest() {
|
||||||
CompletableFuture<String> hutool = CompletableFuture.supplyAsync(() -> {
|
CompletableFuture<String> hutool = CompletableFuture.supplyAsync(() -> {
|
||||||
ThreadUtil.sleep(3, TimeUnit.SECONDS);
|
ThreadUtil.sleep(1, TimeUnit.SECONDS);
|
||||||
return "hutool";
|
return "hutool";
|
||||||
});
|
});
|
||||||
CompletableFuture<String> sweater = CompletableFuture.supplyAsync(() -> {
|
CompletableFuture<String> sweater = CompletableFuture.supplyAsync(() -> {
|
||||||
ThreadUtil.sleep(4, TimeUnit.SECONDS);
|
ThreadUtil.sleep(2, TimeUnit.SECONDS);
|
||||||
return "卫衣";
|
return "卫衣";
|
||||||
});
|
});
|
||||||
CompletableFuture<String> warm = CompletableFuture.supplyAsync(() -> {
|
CompletableFuture<String> warm = CompletableFuture.supplyAsync(() -> {
|
||||||
ThreadUtil.sleep(5, TimeUnit.SECONDS);
|
ThreadUtil.sleep(3, TimeUnit.SECONDS);
|
||||||
return "真暖和";
|
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)));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -400,4 +400,10 @@ public class NumberUtilTest {
|
|||||||
final String s = new BigDecimal(num).toPlainString();
|
final String s = new BigDecimal(num).toPlainString();
|
||||||
Assert.assertEquals("5344342.34", s);
|
Assert.assertEquals("5344342.34", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateBySetTest(){
|
||||||
|
final Integer[] integers = NumberUtil.generateBySet(10, 100, 5);
|
||||||
|
Assert.assertEquals(5, integers.length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user