{"id":59,"date":"2024-02-24T12:39:00","date_gmt":"2024-02-24T12:39:00","guid":{"rendered":"https:\/\/rudnil.uber.space\/?p=59"},"modified":"2025-02-28T12:44:45","modified_gmt":"2025-02-28T12:44:45","slug":"how-to-use-the-executorservice-with-virtual-threads","status":"publish","type":"post","link":"https:\/\/blog.nilsrudolph.de\/?p=59","title":{"rendered":"How to use the ExecutorService with virtual Threads?"},"content":{"rendered":"\n<p>Since Java 21 we have virtual threads. But how can we take advantage of virtual thread wen we use <code>ExecutorService<\/code> or <code>ScheduledExecutorService<\/code>?<\/p>\n\n\n\n<p>For an <code>ExecutorService<\/code> you can just use the factory method <code>Executors.newVirtualThreadPerTaskExecutor()<\/code> or if you want to give the virtual threads a name:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">ThreadFactory threadFactory = Thread.ofVirtual().name(\"virtualThread\").factory();\nExecutorService service = Executors.newThreadPerTaskExecutor(threadFactory);<\/code><\/pre>\n\n\n\n<p>For a <code>ScheduledExecutorService<\/code> you can use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">ThreadFactory threadFactory = Thread.ofVirtual().name(\"virtualThread\").factory();\nScheduledExecutorService service = Executors.newScheduledThreadPool(0,  threadFactory);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Since Java 21 we have virtual threads. But how can we take advantage of virtual thread wen we use ExecutorService or ScheduledExecutorService? For an ExecutorService you can just use the factory method Executors.newVirtualThreadPerTaskExecutor() or if you want to give the virtual threads a name: For a ScheduledExecutorService you can use the following code:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":1,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":60,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/60"}],"wp:attachment":[{"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}