<div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Have you tried using a ThreadFactory factory instead? A custom<br>ThreadFactory can be used to wrap all tasks so you get the<br>acquire/release to limit concurrency.</blockquote><div><br></div><div>I haven't tried that. Would this be the optimal solution with that approach?</div><div><div style="background-color:rgb(25,26,28);color:rgb(188,190,196)"><pre style="font-family:"JetBrains Mono",monospace;font-size:14.3pt"><span style="color:rgb(207,142,109)">class </span>LimitedThreadFactory <span style="color:rgb(207,142,109)">implements </span>ThreadFactory {<br> <span style="color:rgb(207,142,109)">private final </span>Semaphore <span style="color:rgb(199,125,187)">semaphore</span>;<br><br> <span style="color:rgb(207,142,109)">private </span><span style="color:rgb(86,168,245)">LimitedThreadFactory</span>(<span style="color:rgb(207,142,109)">int </span>n) {<br> <span style="color:rgb(207,142,109)">this</span>.<span style="color:rgb(199,125,187)">semaphore </span>= <span style="color:rgb(207,142,109)">new </span>Semaphore(n, <span style="color:rgb(207,142,109)">true</span>);<br> }<br><br> <span style="color:rgb(207,142,109)">static </span>LimitedThreadFactory <span style="color:rgb(86,168,245)">of</span>(<span style="color:rgb(207,142,109)">int </span>n) {<br> <span style="color:rgb(207,142,109)">return new </span>LimitedThreadFactory(n);<br> }<br><br> <span style="color:rgb(179,174,96)">@Override<br></span><span style="color:rgb(179,174,96)"> </span><span style="color:rgb(207,142,109)">public </span>Thread <span style="color:rgb(86,168,245)">newThread</span>(Runnable r) {<br> <span style="color:rgb(207,142,109)">return </span>Thread.<span style="font-style:italic">ofVirtual</span>()<br> .unstarted(() -> {<br> <span style="color:rgb(207,142,109)">try </span>{<br> <span style="color:rgb(199,125,187)">semaphore</span>.acquire();<br> <span style="color:rgb(199,125,187)">r</span>.run();<br> } <span style="color:rgb(207,142,109)">catch </span>(InterruptedException e) {<br> Thread.<span style="font-style:italic">currentThread</span>().interrupt();<br> <span style="color:rgb(207,142,109)">throw new </span>RuntimeException(e);<br> } <span style="color:rgb(207,142,109)">finally </span>{<br> <span style="color:rgb(199,125,187)">semaphore</span>.release();<br> }<br> });<br> }<br>}<br></pre></div></div><div><br></div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Tue, Oct 21, 2025 at 12:06 PM Alan Bateman <<a href="mailto:alan.bateman@oracle.com">alan.bateman@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
On 21/10/2025 08:40, Filip Egeric wrote:<br>
> Hello,<br>
><br>
> I would like to follow up on this one, because I also found myself <br>
> creating a wrapper similar to this one, but for a different reason.<br>
> Specifically, when I want to limit concurrency in order to not exceed <br>
> a rate limit somewhere.<br>
<br>
Have you tried using a ThreadFactory factory instead? A custom <br>
ThreadFactory can be used to wrap all tasks so you get the <br>
acquire/release to limit concurrency.<br>
<br>
-Alan<br>
</blockquote></div>