Java 21: Start a virtual thread directly

Thread.ofVirtual() starts a virtual thread without an executor.

Code
Thread t = Thread.ofVirtual().start(() ->
        System.out.println("hello from " + Thread.currentThread().isVirtual()));
t.join();
Output
hello from true
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-26