前言
JDK27中结构化并发,有了一定的改进
JDK27中的结构化并发
1、allSuccessfulOrThrow(Function<Throwable, R_X>) 子任务失败时,join() 抛的不是 ExecutionException 而是自定义的 OrderException
2、anySuccessfulOrThrow(Function):支持自定义异常等
例子如下
java
import java.time.Duration;
import java.util.List;
import java.util.concurrent.StructuredTaskScope;
import java.util.concurrent.StructuredTaskScope.Joiner;
import java.util.concurrent.StructuredTaskScope.Subtask;
/*
* JDK 27 新语法 ------ JEP 533: Structured Concurrency(第七次预览,需 --enable-preview)
* 相比 JDK 26(JEP 525 第六次预览)的关键变化(javap 27 实测):
* 1) StructuredTaskScope/Joiner 变为三类型参数 <T, R, R_X extends Throwable>,
* join() 只抛 R_X(26 是 throws Throwable),异常类型在编译期可知;
* 2) StructuredTaskScope 从抽象类改为 interface,Joiner.result()/timeout() 是抽象方法(可自定义 Joiner);
* 3) Joiner 工厂新增 Function<Throwable, R_X> 重载:子任务失败可映射为业务异常,join() 抛的就是它;
* 4) open(joiner, UnaryOperator<Configuration>) 支持超时等配置,超时走 joiner.timeout() 同样抛 R_X;
* 5) allUntil(Predicate):任一子任务完成且谓词为 true 立即取消其余任务,join() 返回全部 Subtask 供检查。
*/
public class Jdk27StructuredConcurrencyDemo {
/**
* 演示用业务异常:作为第三个类型参数 R_X,join() 失败/超时时抛出
*/
static class OrderException extends RuntimeException {
OrderException(String message, Throwable cause) {
super(message, cause);
}
}
public static void main(String[] args) throws Exception {
// 1) 27 新语法核心:allSuccessfulOrThrow(Function<Throwable, R_X>)
// 子任务失败时,join() 抛的不是 ExecutionException 而是自定义的 OrderException
try (var scope = StructuredTaskScope.open(
Joiner.<String, OrderException>allSuccessfulOrThrow(
cause -> new OrderException("订单聚合失败", cause)))) {
scope.fork(() -> {
Thread.sleep(50);
return "价格:99.9";
});
scope.fork(() -> {
Thread.sleep(20);
throw new IllegalStateException("库存服务宕机");
});
try {
List<String> results = scope.join();
System.out.println("1) 全部成功: " + results);
} catch (OrderException e) {
System.out.println("1) join() 抛自定义异常: " + e.getMessage()
+ " | 根因: " + e.getCause());
}
}
// 2) anySuccessfulOrThrow(Function):竞速版同样支持自定义 R_X
try (var scope = StructuredTaskScope.open(
Joiner.<String, OrderException>anySuccessfulOrThrow(
cause -> new OrderException("全部数据源失败", cause)))) {
scope.fork(() -> {
Thread.sleep(400);
return "慢源(400ms)";
});
scope.fork(() -> {
Thread.sleep(40);
return "快源(40ms)";
});
System.out.println("2) 竞速赢家: " + scope.join());
}
// 3) allUntil(Predicate):任一子任务成功即取消其余,join() 返回全部 Subtask(可看 state)
try (var scope = StructuredTaskScope.open(
Joiner.<String>allUntil(st -> st.state() == Subtask.State.SUCCESS))) {
scope.fork(() -> {
Thread.sleep(400);
return "慢任务(会被取消)";
});
scope.fork(() -> {
Thread.sleep(30);
return "快任务(30ms)";
});
List<Subtask<String>> all = scope.join();
for (Subtask<String> st : all) {
System.out.println("3) state=" + st.state()
+ (st.state() == Subtask.State.SUCCESS ? " 结果=" + st.get() : ""));
}
}
// 4) open(joiner, config):超时走 joiner.timeout(),抛的同样是 R_X
try (var scope = StructuredTaskScope.open(
Joiner.<String, OrderException>anySuccessfulOrThrow(
cause -> new OrderException("竞速失败/超时", cause)),
cfg -> cfg.withTimeout(Duration.ofMillis(100)))) {
scope.fork(() -> {
Thread.sleep(500);
return "500ms 的慢源";
});
try {
System.out.println("4) " + scope.join());
} catch (OrderException e) {
System.out.println("4) 100ms 超时触发: " + e.getMessage()
+ " | 原因: " + e.getCause());
}
}
}
}
输出为

总结
但是目前结构化并发只是预览属性,还没转正,不建议在正式环境使用,而且使用要开启--enable-preview