[External] : Re: Synchronous executor interface

Attila Kelemen attila.kelemen85 at gmail.com
Fri Aug 11 17:30:32 UTC 2023


>
>
> class Wrap {
>     public static void main(String[] args) {
>         String x = run(() -> "hi", Wrap::withMessage);
>         System.out.println(x);
>     }
>
>     static <T> T run(Supplier<T> s, Function<Supplier<T>, Supplier<T>>
> mapper) {
>         return mapper.apply(s).get();
>     }
>
>     static <T> Supplier<T> withMessage(Supplier<T> s) {
>         return () -> {
>             System.out.println("before");
>             try {
>                 return s.get();
>             } finally {
>                 System.out.println("after");
>             }
>         };
>     }
> }
>

Here you have a single invocation point of the "executor", and it is very
tightly coupled with its user, but normally that is not how people use real
executors (and thus I wouldn't need it for the above). Similarly why an
ExecutorService (assuming only its submit(Callable) method) can't be
implemented with a function, consider the following scenario (ignore the
errors due to the checked exceptions):

```
class MyTool {
  private final SynchronousExecutor executor;

  MyTool(SynchronousExecutor executor) {
    this.executor = executor;
  }

  Path writeHash(Path file) {
    String str = executor.execute(() -> Files.readString(file));

    var hashPath = file.resolveSibling(file.getFileName() + ".hash");
    var hash = Integer.toString(str.hashCode());
    return executor.execute(() -> Files.writeString(hashPath, hash));
  }
}
```

where `SynchronousExecutor` is defined as:

```
interface SynchronousExecutor {
  <T> T execute(Supplier<? extends T> action);
}
```

and maybe implemented similar to what you did:

```
class WithMessagExecutor implements SynchronousExecutor {
  public <T> T execute(Supplier<? extends T> action) {
    System.out.println("before");
    try {
      return action.get();
    } finally {
      System.out.println("after");
    }
  }
}
```

Do this with `Function` or whatever similar. You can't.



>
>
> If by “LTS” you're referring to the support service that Oracle’s sales
> team offer for some releases that they pick to help legacy applications
> then it is unrelated to OpenJDK and the development of features (even if
> the number of people who are using them is larger due to widespread
> misunderstandings). We let sales pick their LTS and try to ignore it in the
> development process.
>
>
It doesn't matter much, because - as far as I have seen - the practical
reality is that a vast amount (I would bet that it is a clear majority) of
people (especially libraries) move from LTS to LTS (for whatever reason, be
it legitimate or not). Though of course, if I were you, I wouldn't bother
myself with it as well.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20230811/267e479b/attachment.htm>


More information about the loom-dev mailing list