<div dir="auto">1. We set a limit of, say, 100 not because we can't have 101 or 200, but because we need to draw a line somewhere, and say: if you are using 101, you are definitely doing something wrong.<div dir="auto"><br></div><div dir="auto">Tasks also have pools - that's bounded queues. Again, we might set the limit of 16k, but that's not because we can't have 32k, but because if you have 16k+1, you are doing something wrong. (Your system is not coping, perhaps) <br><div dir="auto"><br></div><div dir="auto">2. Agreed.</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, 7 Sep 2022, 17:31 Ron Pressler, <<a href="mailto:ron.pressler@oracle.com">ron.pressler@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<div style="word-wrap:break-word;line-break:after-white-space">
1. Those are best practices for *platform* threads, which are expensive and often shared. Virtual threads have a different set of best practices. To get a sense for them, think of virtual threads as simply a representation of a task. We don't have task-pools,
 so we don’t have virtual thread pools.
<div><br>
</div>
<div>2. Semaphores work great — better than pools — and it’s trivial to wrap the thread’s body with code that releases the semaphore. In fact, you can even do that in a trivial implementation of ThreadFactory and pass it to APIs intended for virtual
 threads, like Executors.newThreadPerTaskExecutor and StructuredTaskScope. Thread factories also compose nicely. E.g. here's a pretty sophisticated way to control thread creation with semaphores:</div>
<div><br>
</div>
<div>    <font face="Courier New">ThreadFactory semaphoreThreadFactory(Semaphore s, ThreadFactory tf) {</font></div>
<font face="Courier New">      return r -> {<br>
          try {<br>
              s.acquire();<br>
              return tf.newThread(() -> { try { r.run(); } finally { s.release(); }});<br>
          } catch (InterruptedException e) { throw new RuntimeException(e); }<br>
      };<br>
  }</font>
<div><br>
</div>
<div>— Ron<br>
<div><br>
<blockquote type="cite">
<div>On 7 Sep 2022, at 14:22, Alex Otenko <<a href="mailto:oleksandr.otenko@gmail.com" target="_blank" rel="noreferrer">oleksandr.otenko@gmail.com</a>> wrote:</div>
<br>
<div>
<div dir="auto">Well, threads are different. There are best practices that involve thread pools with pool size limits - not because we are mean, but because we want to be sure we detect runaway computations. With Virtual threads the suggestions sounded
 here were to not pool or reuse them. So we can't use the same technique. Semaphores also won't work, as someone would need to count down when the Virtual thread is done.</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Wed, 7 Sep 2022, 13:12 Ron Pressler, <<a href="mailto:ron.pressler@oracle.com" target="_blank" rel="noreferrer">ron.pressler@oracle.com</a>> wrote:<br>
</div>
<blockquote style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex" class="gmail_quote">
<br>
<br>
> On 7 Sep 2022, at 09:05, Alex Otenko <<a href="mailto:oleksandr.otenko@gmail.com" rel="noreferrer noreferrer" target="_blank">oleksandr.otenko@gmail.com</a>> wrote:<br>
> <br>
> On a different but somewhat related note. What do we get when we can't create a new thread? I think we get an OOME.<br>
> <br>
> Is there a way to limit the number of Virtual threads platform-wide so we get an error that can be handled in some other way than trying to catch and analyze OOME?<br>
<br>
<br>
No, just as there is no way to limit the number of Strings, ArrayLists, CompletableFutures or native byte buffers platform-wide — or platform threads for that matter. Threads are just objects, and virtual threads are objects that are more similar to Strings
 or CompletableFuture in their resource consumption than to platform threads or native buffers, so there’s also no reason to do that any more than for other kinds of objects. But you can monitor the number of Threads in the platform with MBeans (mostly because
 virtual threads are threads, and we provide that service for threads).<br>
<br>
Creation of virtual threads can be controlled by the application using the mechanisms available today to control any kind of object (say, a semaphore).<br>
<br>
— Ron<br>
<br>
<br>
</blockquote>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>

</blockquote></div>