Executors.newVirtualThreadPerTaskScheduledExecutor() ?
Ron Pressler
ron.pressler at oracle.com
Wed Aug 2 13:19:42 UTC 2023
> On 2 Aug 2023, at 12:26, Dr Heinz M. Kabutz <heinz at javaspecialists.eu> wrote:
>
> I've been doing this for a while (and am using the approach on my javaspecialists.eu website):
> Executors.newSingleThreadScheduledExecutor(
> Thread.ofVirtual().factory())
>
> It works very well indeed.
> Regards
>
> Heinz
> —
I would advise against that and in favour of the approach in the Stack Overflow answer. Remember that a virtual thread should normally only ever execute a single task that’s been submitted to an Executor.
I understand that your intent is good, and that you probably just want to use the convenient “fixed rate” methods of ScheduledExecutorService to run what is *conceptually* a single task, but many of the problems we see with people misusing virtual threads and not getting good results are due to them not internalising this idea that a virtual thread is a single task, so I would suggest not to show this kind of usage to virtual thread beginners.
— Ron
More information about the loom-dev
mailing list