Platform vs Virtual Threads
When each is appropriate.
Two Kinds of Threads
Java now has two thread flavors. Platform threads are the classic wrappers around OS threads. Virtual threads are lightweight and JVM-managed.
Knowing when to reach for each is the heart of writing scalable concurrent Java.
Creating a Platform Thread
You create a platform thread with Thread.ofPlatform(), the mirror of Thread.ofVirtual().
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread p = Thread.ofPlatform().name("native-worker").start(() ->
System.out.println("Platform? " + !Thread.currentThread().isVirtual()));
p.join();
}
}All lessons in this course
- What Are Virtual Threads
- Creating Virtual Threads
- Platform vs Virtual Threads
- Pitfalls: Pinning and ThreadLocals