You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java创建新线程的控制流及start()调用后线程执行疑问

Java Thread Execution: Why start() Doesn't Block the Main Thread & Control Flow Explained

First, let's recap your code and output for context:

Your Runnable Implementation Code

class NewThread implements Runnable {
    Thread t;
    NewThread() {
        t = new Thread(this,"Demo Thread");
        System.out.println("Child Thread: " + t);
        t.start();
    }
    public void run() {
        try {
            for(int i = 5; i > 0; i--) {
                System.out.println("Child Thread: " + i);
                t.sleep(500);
            }
        } catch(InterruptedException e) {
            System.out.println("Exception Caught!");
        }
        System.out.println("Exiting child thread!");
    }
}
class Threads {
    public static void main(String args []) {
        new NewThread();
        try {
            for(int i = 5; i > 0; i--) {
                System.out.println("Main thread: " + i);
                Thread.sleep(1000);
            }
        } catch(InterruptedException e) {
            System.out.println("Exception caught!");
        }
        System.out.println("Exiting main thread!");
    }
}

Your Program Output

C:\Users\Kaustubh Srivastava\Desktop\Java\Test>java Threads
Child Thread: Thread[Demo Thread,5,main]
Main thread: 5
Child Thread: 5
Child Thread: 4
Main thread: 4
Child Thread: 3
Child Thread: 2
Main thread: 3
Child Thread: 1
Exiting child thread!
Main thread: 2
Main thread: 1
Exiting main thread!

Why Calling start() Lets the Main Thread Continue (Instead of Running run() Immediately)

When you call t.start(), you're not directly executing the run() method right away. Here's the key breakdown:

  • start() tells the JVM to request the operating system to create a new, separate execution thread. This thread gets added to the OS's thread scheduler queue.
  • The OS scheduler decides when to allocate CPU time to this new thread—your main thread is already running, and the scheduler won't immediately pause it unless there's a priority override (which doesn't apply here, since both threads use the default priority of 5).
  • As soon as the JVM kicks off the request for the new thread, start() returns immediately to the calling thread (your main thread). The main thread then picks up right where it left off: executing the for loop in the main() method.

That's why you see Main thread: 5 print before Child Thread: 5—the main thread didn't wait for the new thread to start executing its run() method; it kept going as soon as start() returned.


Control Flow When Creating a New Thread in Java

Let's walk through the exact sequence of events in your code:

  1. Main thread starts: The JVM launches the main thread, which begins executing the main() method.
  2. NewThread constructor runs: The main thread calls new NewThread(), entering the constructor:
    • Creates a Thread instance tied to your Runnable implementation.
    • Prints the child thread's metadata.
    • Calls t.start(): This triggers the JVM to request a new thread from the OS, then immediately returns to the constructor.
  3. Constructor finishes; main thread resumes: The NewThread constructor exits, and the main thread moves to the try block in main(), printing Main thread: 5 and then sleeping for 1000ms.
  4. Child thread gets scheduled: While the main thread sleeps, the OS scheduler assigns CPU time to the new child thread. The child thread starts executing its run() method:
    • Prints Child Thread: 5, then sleeps for 500ms.
  5. Alternating execution: The child thread wakes up every 500ms to print the next number, while the main thread wakes up every 1000ms to do the same. Since the main thread sleeps twice as long, the child thread prints two numbers for every one the main thread prints (until the child thread finishes its loop).
  6. Threads terminate: The child thread finishes its for loop, prints Exiting child thread!, and terminates. The main thread completes its remaining loop iterations, prints Exiting main thread!, and terminates.

One critical note to reinforce: If you had called t.run() directly instead of t.start(), no new thread would be created—run() would execute in the main thread, and you'd see the child thread's loop finish completely before the main thread's loop even started. start() is the only way to trigger a separate execution thread.

内容的提问来源于stack exchange,提问作者Kaustubh Srivastava

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:50:47