前言
jdk21在2023年9月份就已经发布了,是一个长期支持(LTS)版本,提供五年的Premier支持和扩展支持到2031年9月,而且其发布的新属性,确实值得一看,一下就说一下jdk21的结构化并发
结构化并发
jdk21发布的同时。支持了结构化并发,也就是看见和CompletableFuture一样,提供异步属性,也就是提供了StructuredTaskScope
StructuredTaskScope说明
StructuredTaskScope
支持任务拆分为多个并发子任务,在各自的线程中执行,以及子任务必须在主任务继续之前完成的情况
StructuredTaskScope使用
StructuredTaskScope子任务创建
public class StructuredDemo {
public static void main(String[] args) {
var structuredTaskScope = new StructuredTaskScope();
try (structuredTaskScope) {
StructuredTaskScope.Subtask subtask = structuredTaskScope.fork(() -> {
System.out.println(14);
Thread.sleep(5000);
System.out.println("======1执行完成");
return 12;
});
Supplier stringSupplier = structuredTaskScope.fork(() -> {
System.out.println(15);
return "aa";
});
structuredTaskScope.join();
Integer result = subtask.get();
System.out.println("结果为:" + result);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(10);
}
}
以上会等待每个子任务完成,然后往下执行,需要使用join()等待,可以单独获取每个子任务的返回结果
ShutdownOnSuccess使用
ShutdownOnSuccess是StructuredTaskScope提供的final修饰的静态类,其作用是,当一个子任务完成时,那么就算其他任务完成了
public class Structured1Demo {
public static void main(String[] args) {
var structuredTaskScope = new StructuredTaskScope.ShutdownOnSuccess();
try (structuredTaskScope) {
StructuredTaskScope.Subtask subtask = structuredTaskScope.fork(() -> {
System.out.println(14);
Thread.sleep(5000);
System.out.println("======1执行完成");
return 12;
});
Supplier stringSupplier = structuredTaskScope.fork(() -> {
System.out.println(15);
return "aa";
});
structuredTaskScope.join();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(10);
}
}
这时结果为"======1执行完成"是不会输出的,也是可以获取子任务的返回结果
public class Structured1Demo {
public static void main(String[] args) {
var structuredTaskScope = new StructuredTaskScope.ShutdownOnSuccess();
try (structuredTaskScope) {
StructuredTaskScope.Subtask subtask = structuredTaskScope.fork(() -> {
System.out.println(14);
Thread.sleep(5000);
System.out.println("======1执行完成");
return 12;
});
Supplier stringSupplier = structuredTaskScope.fork(() -> {
System.out.println(15);
return "aa";
});
structuredTaskScope.join();
String data = stringSupplier.get();
System.out.println(data);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(10);
}
}
但是需要注意的是,ShutdownOnSuccess是当一个子任务完成时,整个任务就完成了,所以如果获取的子任务没有执行完成,就会抛异常
ShutdownOnFailure使用
ShutdownOnFailure是StructuredTaskScope提供的final修饰的静态类,其作用是,当一个子任务失败时,那么就算其他任务完成了
public class Structured1Demo {
public static void main(String[] args) {
var structuredTaskScope = new StructuredTaskScope.ShutdownOnFailure();
try (structuredTaskScope) {
StructuredTaskScope.Subtask subtask = structuredTaskScope.fork(() -> {
System.out.println(14);
Thread.sleep(5000);
System.out.println("======1执行完成");
return 12;
});
Supplier stringSupplier = structuredTaskScope.fork(() -> {
System.out.println(15);
int a = 1 / 0;
return "aa";
});
structuredTaskScope.join();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(10);
}
}
获取子任务的结果也是一样的,但是这样写是获取不了异常抛出的,要获取子任务异常信息,可以这么写
public class Structured1Demo {
public static void main(String[] args) {
var structuredTaskScope = new StructuredTaskScope.ShutdownOnFailure();
try (structuredTaskScope) {
StructuredTaskScope.Subtask subtask = structuredTaskScope.fork(() -> {
System.out.println(14);
Thread.sleep(5000);
System.out.println("======1执行完成");
return 12;
});
Supplier stringSupplier = structuredTaskScope.fork(() -> {
System.out.println(15);
int a = 1 / 0;
return "aa";
});
structuredTaskScope.join().throwIfFailed();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(10);
}
}
或者这么写
package cn.com.ut.october.pool;
import java.util.concurrent.StructuredTaskScope;
import java.util.function.Supplier;
/**
* @author kaolvkaolv
* @date 2023/10/15 13:13
* @description 结构化并发
*/
public class Structured1Demo {
public static void main(String[] args) {
var structuredTaskScope = new StructuredTaskScope.ShutdownOnFailure();
try (structuredTaskScope) {
StructuredTaskScope.Subtask subtask = structuredTaskScope.fork(() -> {
System.out.println(14);
Thread.sleep(5000);
System.out.println("======1执行完成");
return 12;
});
Supplier stringSupplier = structuredTaskScope.fork(() -> {
System.out.println(15);
int a = 1 / 0;
return "aa";
});
structuredTaskScope.join().throwIfFailed(e->{
System.out.println(e.getMessage());
return new RuntimeException("失败");
});
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(10);
}
}
可以返回自定义异常信息
总结
以前在使用异步的时候,我们要控制子任务的完成信息时,是很难做到的,jdk21提供了结构化并发,可以帮我们提高开发效率,但是在开发过程是,并不一定要使用这种才能实现,毕竟技术这玩意,实现方式有很多种