RUNNABLE vs Actually Running: The Scheduler's Role
What you'll learn: You'll understand why a thread in RUNNABLE state isn't necessarily executing right now, and how the operating system decides which threads actually get CPU time.
The Confusion Around RUNNABLE
When a Java thread is in the RUNNABLE state, it means the thread is eligible to run—not that it's currently executing instructions on a CPU core. This is a crucial distinction that catches many developers off guard.
Think of it like passengers waiting at a bus stop. Everyone standing there is "ready to board" (RUNNABLE), but only those who fit on the bus that just arrived are actually traveling (running). The bus driver decides who gets on.
The OS Thread Scheduler's Role
Your operating system has a component called the thread scheduler that decides which RUNNABLE threads get actual CPU time. Even if you have 100 RUNNABLE threads, if your machine has 8 cores, only 8 can be actively executing at any given microsecond.
The scheduler uses various algorithms (often time-slicing with priority levels) to rapidly switch between RUNNABLE threads, giving each a turn on the CPU. This switching happens so fast—often thousands of times per second—that it creates the illusion of parallelism, even on a single core.
What This Means for Your Code
When you call start() on a thread, it enters the RUNNABLE state almost immediately. But it might wait milliseconds (or longer) before the scheduler actually assigns it to a CPU core. Similarly, a running thread can be preempted—yanked off the CPU—and put back in the RUNNABLE queue if the scheduler decides another thread deserves a turn.
You have limited control over these scheduling decisions from Java. The JVM and OS handle the details.
Key Takeaway: RUNNABLE means "ready and waiting for CPU time," not "currently executing." The OS thread scheduler determines which RUNNABLE threads actually run and when they run, creating the multitasking behavior you observe.