<div dir="ltr"><div>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):</div><div><br></div><div>```</div><div>ThreadFactory limitedFactory(int limit, ThreadFactory wrapped) {<br> var semaphore = new Semaphore(limit);<br> return r -> {<br> var thread = wrapped.newThread(() -> {<br> try {<br> r.run();<br> } finally {<br> semaphore.release();<br> }<br> });<br> semaphore.acquireUninterruptibly();<br> return thread;<br> };<br>}<br></div><div>```</div><div><br></div><div>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.</div><div><br></div><div>Attila</div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">Benoit LEFEVRE -CAMPUS- <<a href="mailto:benoit.lefevre@decathlon.com">benoit.lefevre@decathlon.com</a>> ezt írta (időpont: 2025. dec. 23., K, 19:59):<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello<div><br></div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>Thanks Attila & Alan for your feedback, and sorry for this late reply of mine.<br><br>Weeks before Christmas are pretty busy at work :p</div><div><br></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">@Attila : I quickly thought about something like what you advise.<br><br>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.<br><br>If it's still possible with it, I have most probably missed something. <br><br>I remember that in its previous version, there were some kind of callbacks methods whose implementations could be overloaded.<br><br>But now the API is much more composition oriented, where do you advise to code this ?<br><br>@Alan : Do you have the references of those discussions by any changes ?<br><br>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.<br></blockquote></div></div>
</blockquote></div></div>