What are virtual threads in Java, and how do they differ from platform threads?
A virtual thread is a thread that the virtual machine manages, and the operating system never sees it.
Every thread that you write today is a platform thread, and each one of them is a thread in the operating system. That has two costs. Creating one is not free, because the operating system has to set up the structures for it. And the memory that it uses is the memory that the operating system reserves for the stack, which is usually a megabyte or more.
A virtual thread costs almost nothing to create, because it is a small object on the heap and the virtual machine schedules it onto a much smaller number of platform threads, which are called carrier threads. The virtual thread is not tied to a carrier thread for its whole life. When it waits for something, the carrier thread is free to run another virtual thread.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) throws Exception {
final AtomicInteger done = new AtomicInteger();
// One virtual thread for each task. They are cheap to make.
try (ExecutorService pool = Executors.newVirtualThreadPerTaskExecutor()) {
for (int index = 0; index < 1000; index++) {
pool.submit(() -> {
try {
// A blocking call is fine in a virtual thread.
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
done.incrementAndGet();
});
}
}
System.out.println("tasks finished : " + done.get());
}
}
So what does that change in practice? The big one is that blocking becomes cheap. With platform threads, a thread that waits for a network response is a thread that does nothing while it waits, so you need many of them and you pay the memory cost for each one. With virtual threads, you can have a million of them, because they are small and because the ones that are waiting do not hold on to a carrier thread.
So the answer to the question is this. A virtual thread is a cheap thread that the virtual machine schedules onto a small number of carrier threads, and it makes the old advice about not blocking in a thread irrelevant.
The next question in this topic looks at how to make one and when to use them.