<div dir="ltr">I guess one problem is that there are multiple ways to implement such a SES (and it is not obvious to me which is the best).<div><br></div><div>1. For every schedule* method, SES submits a new task to a thread per task executor which simply sleeps before executing the task.</div><div>2. The virtual thread based SES has an underlying single threaded SES (also using VT), and all scheduled tasks are scheduled by the wrapped SES, but the tasks are wrapped into another one which - instead of directly executing the actual task - just submits it to a thread per task executor.</div><div><br></div><div>The above two are actually not that hard to implement (though a bit cumbersome at places, especially the 2nd), if really needed.</div><div><br></div><div>That said, I think VT based code should just use a simple `ExecutorService`, and loop as it sees convenient with sleeps (of course, this might not be possible, if you rely on an API needing a SES). In fact, instead of a SES implementation, I would rather see convenient utilities for repeated synchronous task execution. Though I believe this would first require a sensible cancellation mechanism in the JDK (as opposed to the awkward interrupt based one).</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Formula Salt <<a href="mailto:formulasalt410@gmail.com">formulasalt410@gmail.com</a>> ezt írta (időpont: 2023. aug. 1., K, 7:58):<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">Hi,<div><br></div><div>I am trying to use virtual threads together with the ScheduledExecutorService but I am uncertain what is the best way to do so. To use the ExecutorService with virtual threads we would use Executors.newVirtualThreadPerTaskExecutor() but there seems to be no equivalent method for the ScheduledExecutorService. </div><div><br></div><div>Executors.newScheduledThreadPool(int, ThreadFactory) would require pooling virtual threads which I believe is considered bad practice.</div><div>Executors.newSingleThreadScheduledExecutor(ThreadFactory) only allows for a single thread so tasks must execute sequentially.</div><div><br></div><div>The best way I've found so far is in this StackOverflow answer (<a href="https://stackoverflow.com/a/76599122" target="_blank">https://stackoverflow.com/a/76599122</a>) which suggests using both a single-threaded ScheduledExecutorService and Executors.newVirtualThreadPerTaskExecutor(). This works but perhaps the Executors API should directly support this via Executors.newVirtualThreadPerTaskScheduledExecutor() ?</div></div>
</blockquote></div>