在A中,调用B.join()那么,A会“等”B先执行
public class Join01 extends Thread { @Override public void run() { int sum =0; for(int i=0;i<10000000;i++){ sum+=1; } System.out.println(sum+":@[email protected]"); } public static void main(String[] args) throws InterruptedException { Join01 t = new Join01(); t.start(); for(int i=0;i<100;i++){ if(i==18){t.join();} System.out.println(i+":main"); } } }
结果:
0:main
1:main
2:main
3:main
4:main
5:main
6:main
7:main
8:main
9:main
10:main
11:main
12:main
13:main
14:main
15:main
16:main
17:main
10000000:@[email protected]
18:main
19:main.。。。
时间: 2024-10-17 19:31:27