Executors.newVirtualThreadPerTaskScheduledExecutor() ?
Ron Pressler
ron.pressler at oracle.com
Tue Aug 1 16:17:08 UTC 2023
Hi.
I think that the approach in the Stack Overflow answer is perfectly fine. We may end up incorporating something like it into the standard library at some point.
Alternatively, you can just loop and sleep, although the implementation of sleep for virtual threads is not, at present, as efficient as we’d like it to be. We’re working on that.
— Ron
> On 1 Aug 2023, at 06:57, Formula Salt <formulasalt410 at gmail.com> wrote:
>
> Hi,
>
> 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.
>
> Executors.newScheduledThreadPool(int, ThreadFactory) would require pooling virtual threads which I believe is considered bad practice.
> Executors.newSingleThreadScheduledExecutor(ThreadFactory) only allows for a single thread so tasks must execute sequentially.
>
> The best way I've found so far is in this StackOverflow answer (https://stackoverflow.com/a/76599122) 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() ?
More information about the loom-dev
mailing list