Replace blocking platform threads with virtual threads

Proprietary

Replaces new Thread(task) and new Thread(task, name) with Thread.ofVirtual().unstarted(task) and Thread.ofVirtual().name(name).unstarted(task) in code on Java 21 or later, when the task is a lambda or anonymous Runnable that calls Thread.sleep(long), HttpURLConnection#getResponseCode() or getResponseMessage(). A platform thread holds an operating system thread for as long as the call blocks, where a virtual thread releases it. The thread must be held in a local variable or private field, made a daemon with setDaemon(true) right after it is created, as a virtual thread always is, and otherwise only named, started, interrupted, joined or queried. A virtual thread created without a name has an empty name instead of Thread-N. Left unchanged: tasks with a synchronized block or method, a wait or notify call, or another use of Thread.currentThread() than those calls; tasks that use a URLConnection or socket on a thread that is interrupted, which would close a virtual thread's socket; named threads whose anonymous task has fields or initializers, which the rewrite would run after the name; Thread subclasses; Thread.ofPlatform(); blocking calls only in work handed on to an executor or another thread; and test sources.

io.moderne.staticanalysis.ReplacePlatformThreadWithVirtualThread
mod run . --recipe=io.moderne.staticanalysis.ReplacePlatformThreadWithVirtualThread

Install it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.