Synchronous executor interface

Attila Kelemen attila.kelemen85 at gmail.com
Sun Aug 6 21:33:27 UTC 2023


Robert Engels <rengels at ix.netcom.com> ezt írta (időpont: 2023. aug. 6., V,
23:27):

> But tbh - I don’t really know what you are asking for because you talk
> about limiting the number of requests but making it called on the same
> thread. I don’t even think that is possible.
>
>
A possible implementation of such an interface could limit concurrency with
a semaphore like this:

```
class LimitedSynchronousExecutor implements SynchronousExecutor {
  private final Semaphore semaphore;

  public LimitedSynchronousExecutor(int maxConcurrency) {
    semaphore = new Semaphore(maxConcurrency);
  }

  @Override
  public <T> T execute(Supplier<? extends T> action) {
    semaphore.acquireUninterruptibly();
    try {
      return action.get();
    } finally {
      semaphore.release();
    }
  }
}
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20230806/bf810e17/attachment.htm>


More information about the loom-dev mailing list