我正试图理解目前瓦 Java21中正在审查的有条理的一致意见(JEP 453 )。
我有这个例子:使用<代码>的Im。 ShutdownOn Success and ShutdownOnFailure
。
如java docs所述,
ShutdownOn Success抓住了第一个结果,关闭了工作范围,以打断未完工的路面,并提醒所有人。 这一类别针对的是任何子公司(“任何”)的结果以及不需要等待其他未完成任务结果的情况。 它界定了取得第一种结果的方法,或者如果所有的子公司都失败,就放弃一种例外。
ShutdownOnFailure抓住了第一个例外,并缩小了任务范围。 这一类别针对的是需要所有子公司(“所有”)结果的情况;如果任何子公司未能做到这一点,则不再需要其他未完成子公司的结果。 如果亚特克人中任何一个都失败,如果界定了放弃例外的方法。
what I understand is in ShutdownOnSuccess
case if one task get completed rest will be interrupted and in
ShutdownOnFailure
all task needs to be completed , then shutdown call is made.
因此,完成交给成功的任务的时间将减少失败。 这种理解是否正确?
如果是,那么完成时间 成功比每次处决失败大,原因何在?
这里是样本代码。
package com.example.threadex;
import java.time.Duration;
import java.time.Instant;
import java.util.Random;
import java.util.concurrent.StructuredTaskScope;
import java.util.concurrent.StructuredTaskScope.Subtask;
public class StructuredTaskScopeExample {
public static void main(String[] args) {
Instant start = Instant.now();
try(var scope = new StructuredTaskScope.ShutdownOnSuccess<String>()){
Subtask<String> task1 = scope.fork(Weather::getTempFromA);
Subtask<String> task2 = scope.fork(Weather::getTempFromB);
Subtask<String> task3 = scope.fork(Weather::getTempFromC);
scope.join();
System.out.println(STR."""
task1: {task1.state()}: result : {task1.state() == Subtask.State.SUCCESS ? task1.get() : "Not Available"}
task2: {task2.state()}: result : {task2.state() == Subtask.State.SUCCESS ? task2.get() : "Not Available"}
task3: {task3.state()}: result : {task3.state() == Subtask.State.SUCCESS ? task3.get() : "Not Available"}
""");
} catch (Throwable e) {
throw new RuntimeException(e);
}
System.out.println(STR."AnySuccess : {Duration.between(start, Instant.now()).toMillis()}ms");
System.out.println("==================");
Instant start1 = Instant.now();
try(var scope1 = new StructuredTaskScope.ShutdownOnFailure()){
Subtask<String> task1 = scope1.fork(Weather::getTempFromA);
Subtask<String> task2 = scope1.fork(Weather::getTempFromB);
Subtask<String> task3 = scope1.fork(Weather::getTempFromC);
scope1.join();
scope1.throwIfFailed(RuntimeException::new);
System.out.println(STR."""
task1: {task1.state()}: result: {task1.get()}
task2: {task2.state()}: result: {task2.get()}
task3: {task3.state()}: result: {task3.get()}
""");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(STR."AllSuccess : {Duration.between(start1, Instant.now()).toMillis()}ms");
}
static class Weather{
static Random random = new Random();
public static String getTempFromA(){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return STR."Temp from A: Temp = {random.nextInt(0, 100)}";
}
public static String getTempFromB(){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return STR."Temp from B: Temp = {random.nextInt(0, 100)}";
}
public static String getTempFromC(){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return STR."Temp from C: Temp = {random.nextInt(0, 100)}";
}
}
}
Output i m getting:
task1: SUCCESS: result : Temp from A: Temp = 57
task2: SUCCESS: result : Temp from B: Temp = 20
task3: SUCCESS: result : Temp from C: Temp = 45
AnySuccess : 57ms
==================
task1: SUCCESS: result: Temp from A: Temp = 78
task2: SUCCESS: result: Temp from B: Temp = 54
task3: SUCCESS: result: Temp from C: Temp = 59
AllSuccess : 18ms
谁能解释这一区别?
成就