Sub topic 1
Platform Threads: What Java Threads Have Always Been
Before you learn about virtual threads, you need a clear picture of the threads Java has always had. This chapter explains what they are, how they work, and why they cost more than you might expect.
What Is a Thread?
A thread is a path of execution inside a program. It runs your code one step at a time. A program with several threads can do several things at once. For example, one thread can download a file while another updates the screen.
Every Java program starts with one thread, called main. You can start more threads yourself.
What Is a Platform Thread?
A platform thread is a Java thread that is tied to one operating system thread. An operating system thread (OS thread) is a thread that the operating system, such as Linux, Windows, or macOS, creates and manages.
Before Java 21, every Java thread was a platform thread. Today, new Thread(...) still creates one. The link is one-to-one: one Java thread uses one OS thread for its whole life.
The scheduler is the part of the operating system that decides which thread runs on which CPU core, and for how long. Java does not make that choice for platform threads. The OS makes it.
A Worked Example
This program starts one platform thread and waits for it to finish.
public class HelloPlatform {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(() -> {
Thread current = Thread.currentThread();
System.out.println("Running in " + current.getName()
+ ", virtual = " + current.isVirtual());
}, "worker-1");
worker.start();
worker.join();
System.out.println("CPU cores available: "
+ Runtime.getRuntime().availableProcessors());
}
}Here is what happens:
new Thread(...)creates a thread object with a task (a lambda) and the nameworker-1.start()asks the operating system to create a real OS thread and run the task on it.join()makesmainwait until the worker finishes.isVirtual()printsfalse, because this is a platform thread.
Why Platform Threads Are Expensive
Platform threads work well, but each one has a real cost.
| Cost | What it means |
|---|---|
| Memory | Each thread reserves its own **stack**, which is the memory that holds method calls and local variables. On 64-bit systems the default reservation is often about 1 MB. |
| Creation time | Starting a thread needs a call into the operating system, which is much slower than creating a normal Java object. |
| Switching | When the OS moves a core from one thread to another, it saves and restores state. This is called a **context switch**, and it takes time. |
| Limits | The OS caps how many threads a process can have. Most machines handle thousands of threads, not millions. |
Because threads are costly, Java programs have long reused them. A thread pool is a fixed group of threads that takes tasks from a queue, so the program does not create a new thread for every task. You will see why pools became necessary, and where they fall short, in the next chapter.
Exercises
Exercise
What does it mean that a platform thread has a one-to-one link with an OS thread?
Show answer
Each Java platform thread is backed by exactly one operating system thread for its whole life, and the operating system schedules that thread onto CPU cores.
Exercise
A program starts 5,000 platform threads that each spend most of their time waiting for a database. Name two costs these threads still impose while they wait.
Show answer
Each waiting thread still holds the memory reserved for its stack (often about 1 MB) and still uses one of the limited OS threads. Creating all 5,000 also took 5,000 slow calls into the operating system.