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

Java中join()方法前的代码为何不先于被join线程执行?

Understanding Thread.join() Behavior with Preceding Code

Great question! Let's break this down clearly, using your code as a reference (I'll fill in the missing parts to make it a complete, runnable example first):

class MyJoinThread extends Thread {
    public static Thread myMain;

    public void run() {
        // Code BEFORE calling join()
        for(int i = 0; i< 2; i++) {
            System.out.println("I am loop");
        }

        try {
            // Wait for the main thread to finish
            myMain.join();
        } catch(InterruptedException ex) {
            ex.printStackTrace();
        }

        // Code AFTER calling join()
        for(int j = 0; j < 5; j++) {
            System.out.println("I am after join loop: " + j);
        }
    }

    public static void main(String[] args) {
        myMain = Thread.currentThread(); // Assign main thread reference
        MyJoinThread t = new MyJoinThread();
        t.start();

        // Main thread's own code
        for(int k = 0; k < 3; k++) {
            System.out.println("I am main thread loop: " + k);
        }
        System.out.println("Main thread is done!");
    }
}

Core Rule to Remember

join() only blocks the thread that calls it, and only after the code before join() has finished executing. The code that runs before you call join() will execute normally, with no blocking—just like any other thread code.

Step-by-Step Execution Flow

Let's walk through what happens when you run this code:

  • The main thread starts, assigns its own reference to myMain, then creates and starts the MyJoinThread instance t. Now two threads are running concurrently: the main thread and t.
  • Before join() is called:
    • Thread t will fully execute its first loop (printing "I am loop" twice) without any interruption. This code runs independently of the main thread—they're both doing their own work at the same time.
    • Meanwhile, the main thread is running its own loop, printing "I am main thread loop: 0/1/2" followed by "Main thread is done!".
  • When join() is called:
    • Thread t hits myMain.join(), and immediately enters a blocked state. It will stay blocked until the myMain thread (the main thread) terminates.
      • If the main thread has already finished by this point (e.g., it printed "Main thread is done!" before t got to join()), t will unblock right away and move to the next code block.
      • If the main thread is still running, t will wait patiently until the main thread finishes all its work.
  • After join() completes:
    • Once the main thread is done, t unblocks and runs its second loop, printing "I am after join loop: 0" through "4".

Example Output (Order May Vary Slightly)

Since the first two loops run concurrently, the order of "I am loop" and "I am main thread loop" lines might shift, but you'll always see the after join lines come after the main thread finishes:

I am loop
I am main thread loop: 0
I am loop
I am main thread loop: 1
I am main thread loop: 2
Main thread is done!
I am after join loop: 0
I am after join loop: 1
I am after join loop: 2
I am after join loop: 3
I am after join loop: 4

To put it simply: join() is a "wait checkpoint" in your thread's execution. Everything before the checkpoint runs normally, and at the checkpoint, your thread pauses until the target thread finishes.

内容的提问来源于stack exchange,提问作者Moytri Barman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:00:51