Java中join()方法前的代码为何不先于被join线程执行?
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 theMyJoinThreadinstancet. Now two threads are running concurrently: the main thread andt. - Before
join()is called:- Thread
twill 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!".
- Thread
- When
join()is called:- Thread
thitsmyMain.join(), and immediately enters a blocked state. It will stay blocked until themyMainthread (the main thread) terminates.- If the main thread has already finished by this point (e.g., it printed "Main thread is done!" before
tgot tojoin()),twill unblock right away and move to the next code block. - If the main thread is still running,
twill wait patiently until the main thread finishes all its work.
- If the main thread has already finished by this point (e.g., it printed "Main thread is done!" before
- Thread
- After
join()completes:- Once the main thread is done,
tunblocks and runs its second loop, printing "I am after join loop: 0" through "4".
- Once the main thread is done,
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

