Thread Start Versus Run Call

Calling run() directly stays on the current thread; only start() creates a worker thread, shown by printing each execution thread name.

JavaData Integrity

Code

snippet
public class ThreadStartVersusRun {

    static class NamedWorker extends Thread {
        @Override
        public void run() {
            System.out.println("executed in: " + Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        // 直接调 run():只是普通方法调用,仍在当前线程顺序执行
        NamedWorker directCall = new NamedWorker();
        directCall.run();

        // start() 才会创建新线程并发执行
        NamedWorker started = new NamedWorker();
        started.start();
        started.join(); // 等它跑完,保证输出顺序稳定

        System.out.println("main thread: " + Thread.currentThread().getName());
    }
}

How it works

One way to read it: the classes ThreadStartVersusRun and NamedWorker and the function run, doing all the work.

What to watch for

  • Zero-fetch snippet: every identifier resolves inside the language distribution.

Putting it to use

Entry signature: public void run()

Each run prints the 2 labeled blocks executed in:, main thread: in source order.

main is the driver: it invokes run before returning.

Core lines

NamedWorker directCall = new NamedWorker();
NamedWorker started = new NamedWorker();
started.join(); // 等它跑完,保证输出顺序稳定
System.out.println("main thread: " + Thread.currentThread().getName());

Interactive Tools for This Topic

FAQ

What does Thread Start Versus Run Call print when run?
The script runs standalone; its 2 labeled outputs include `executed in:`, `main thread:`.