try-with-resources vs structured concurrency

Remi Forax forax at univ-mlv.fr
Sat Nov 20 13:57:23 UTC 2021


Playing with the StructuredExecutor,
i've trouble to write the correct code when i use a structured executor inside a structured executor,
so in several tests, I wrote something like this

    try(var executor = StructuredExecutor.open()) {
      try(var executor2 = StructuredExecutor.open()) {
        var handler = new StructuredExecutor.ShutdownOnSuccess<Integer>();
        executor2.fork(() -> 3, handler);
        executor2.fork(() -> 7, handler);
        executor.join();     // <--- oops
        System.out.println(handler.result());
      }
      executor.join();
    }

sadly, there is no way in Java to hide a local variable with another one
   try(var executor = StructuredExecutor.open()) {
      try(var executor = StructuredExecutor.open()) {  // reuse executor here

even if this is what we want here.

Rémi


More information about the loom-dev mailing list