Card 03/ 08

GotchaDifficulty: Intermediate1 min

Thread.run() vs Thread.start(): Why Calling run() Starts Nothing

Four threads created, no speed-up, and every line of work running on main.

start() creates a new thread and has it call run(). Calling run() yourself is an ordinary method call: the work happens on the thread you are already on, and no new thread is created.

The symptom is a program that creates four threads and runs no faster than before. The work is definitely happening, as the logs show, and all of it happens on one thread:

java
Thread worker = new Thread(() -> {
    System.out.println("on " + Thread.currentThread().getName());
});

worker.run();
worker.start();
text
on main
on Thread-0

run() is an ordinary method. Calling it calls it, on whatever thread you are already on, exactly as calling any other method would. Nothing about a Thread object makes its methods special.

start() is the one that asks the virtual machine for a new thread and has that thread call run(). It is the only line in Java that creates one.

Print Thread.currentThread().getName() when you are not sure. If every line says main, nothing is running concurrently, whatever the code looks like at first glance.