The example CollectingScope in the javadoc of STS is dubious

Remi Forax forax at univ-mlv.fr
Sat May 13 06:06:12 UTC 2023


Hi,
the example CollectingScope in the javadoc of STS
  https://cr.openjdk.org/~alanb/sc/api/java.base/java/util/concurrent/StructuredTaskScope.html

supposes that a Callable as the method equals/hashCode correctly defined but there is a good chance that collectingScope.fork() takes a lambda as parameter and equals (== really) on a lambda is defined as "depending on the implementation".

By example,
  try(var scope = new CollectingScope<Integer>()) {
    scope.fork(() -> 2);
    scope.fork(() -> 2);
    scope.join();
    var map = scope.results();
    System.out.println(map.size());
  }

can print either 1 or 2 depending on the compiler (is it twice the same lambda or not ?).

And obviously if the lambda inside fork() does side effect, you can have the same lambda but with two different results

  private static final AtomicInteger COUNTER = new AtomicInteger();
  ...

  try(var scope = new CollectingScope<Integer>()) {
    scope.fork(COUNTER::getAndAdd);
    scope.fork(COUNTER::getAndAdd);
    scope.join();
    var map = scope.results();
    System.out.println(map.size());
  }

In fact, I believe that providing the Callable in TaskHandle is not that interesting and may lead users to shooting themselves in the foot.

regards,
Rémi


More information about the loom-dev mailing list