Helius
2021-06-29 d95f158b9dfa0149e74bc60a0c5a386dbbdad484
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
package com.xzx.gc;
 
public class Main {
    public static void main(String[] args) {
        WorkThread workThread1 = new WorkThread();
        WorkThread workThread2 = new WorkThread();
        workThread1.start();
        workThread2.start();
        //阻塞Main线程,执行子线程workThread1和workThread2,完毕后继续执行后续的逻辑
        try {
            System.out.println("1"+Thread.currentThread().getName());
            workThread1.join();
            System.out.println("2"+Thread.currentThread().getName());
            workThread2.join();
            System.out.println("3"+Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        System.out.println("run next process.");
    }
 
}
 
 class WorkThread extends Thread {
    @Override
    public void run() {
        try {
            System.out.println(getName() + "run start.");
            //模拟完成子任务执行的时间
            sleep(1000);
            System.out.println(getName() + "run finished.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}