forked from lihengming/java-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoinTest.java
More file actions
38 lines (31 loc) · 1.18 KB
/
Copy pathJoinTest.java
File metadata and controls
38 lines (31 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package thread;
import java.util.concurrent.TimeUnit;
/**
* Created by on 2017/6/16.
*/
public class JoinTest {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> System.out.println(Thread.currentThread().getName() + " Work End 0"));
thread1.start();
thread1.join();//合并到主线程,主线程将等待该子线程执行完毕才会执行
Thread thread2 = new Thread(() -> {
System.out.println(Thread.currentThread().getName() + " Work End 1");
try {Thread.sleep(1000);}
catch (InterruptedException e) {}}
);
thread2.start();
// thread1.join();//合并到主线程,主线程将等待该子线程执行完毕才会执行
thread2.join();//合并到主线程,主线程将等待该子线程执行完毕才会执行
System.out.println("Main Thread Work End");
/**
输出:
Thread-0 Work End
Thread-1 Work End
Main Thread Work End
不使用join():
Main Thread Work End
Thread-0 Work End
Thread-1 Work End
*/
}
}