[StructuredTaskScope] Poor man's "back pressure" ?
Attila Kelemen
attila.kelemen85 at gmail.com
Tue Dec 23 21:58:56 UTC 2025
Holo's response is probably your best bet for STS. Otherwise, Alan alluded
to an implementation like this (disclaimer: I just typed it into an email,
did no testing in anyway):
```
ThreadFactory limitedFactory(int limit, ThreadFactory wrapped) {
var semaphore = new Semaphore(limit);
return r -> {
var thread = wrapped.newThread(() -> {
try {
r.run();
} finally {
semaphore.release();
}
});
semaphore.acquireUninterruptibly();
return thread;
};
}
```
but as I noted previously, this has the problem that no API makes a general
promise that a thread will actually be started. I personally prefer to do a
similar thing with `Executor`, the downside there is that some executors do
not guarantee that they will actually run your task (in which case you will
run out of permits), but many do, so you can just use such an executor.
Attila
Benoit LEFEVRE -CAMPUS- <benoit.lefevre at decathlon.com> ezt írta (időpont:
2025. dec. 23., K, 19:59):
> Hello
>
> Thanks Attila & Alan for your feedback, and sorry for this late reply of
> mine.
>
> Weeks before Christmas are pretty busy at work :p
>
> @Attila : I quickly thought about something like what you advise.
>
> But even if I know that in such a case I should make some kind of "atomic
> assertion or +1" after each .fork() calls, I don't have a clue upon which
> event I have room to make the corresponding "atomic assertion & -1" with
> the new API.
>
> If it's still possible with it, I have most probably missed something.
>
> I remember that in its previous version, there were some kind of callbacks
> methods whose implementations could be overloaded.
>
> But now the API is much more composition oriented, where do you advise to
> code this ?
>
> @Alan : Do you have the references of those discussions by any changes ?
>
> As is, it's not straightforward for me to understand how the ThreadFactory
> can keep track or be warned whenever a thread it created complete.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20251223/c86a3fb6/attachment-0001.htm>
More information about the loom-dev
mailing list