A Java 19 ShutdownOnFailure scope also allows for explicit cancellation of all tasks using the shutdown method. How can I know if the scope has been shutdown? The API includes an isShutdown method, but it is private.
Here is some (incomplete) code just to illustrate a possible use, where a scope is canceled some time after starting by the owner calling shutdown.
I guess that after a join() I could issue another scope.fork() and check if if returns a future to a canceled task but that seems weird. Is that the way to go?
ExecutorService executor = Executors.newCachedThreadPool();
try (StructuredTaskScope.ShutdownOnFailure scope = new StructuredTaskScope.ShutdownOnFailure()) {
Future<Integer> numOrders = scope.fork(Business::getNumItemsFromRequest);
Future<Double> price = scope.fork(Business::getPriceFromDB);
Business.sleepFor(500);
scope.shutdown(); // cancels all the scope tasks
//numOrders.cancel(true); // cancel just one task
scope.join(); // wait for all tasks
try {
scope.throwIfFailed();
// how would I know if scope was shutdown before all tasks completed?
System.out.println("Scope Completed Without Errors ( But possibly canceled ) ");
double amount = numOrders.get() * price.get();