线程执行顺序 join()

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

public class T {
    @SneakyThrows
    public static void main(String[] args) {
        Object o = new Object();
        Thread thread1 = new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "运行");
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + "运行 over");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }, "线程1");

        Thread thread2 = new Thread(() -> {
            try {
                thread1.join();
                System.out.println(Thread.currentThread().getName() + "运行");
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + "运行over");
            } catch (Exception ex) {

            }
        }, "线程2");

        Thread thread3 = new Thread(() -> {
            try {
                thread2.join();
                System.out.println(Thread.currentThread().getName() + "运行");
            } catch (Exception ex) {

            }
        }, "线程3");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}
posted @ 2024-08-14 11:03  干饭达人GoodLucy  阅读(2)  评论(0编辑  收藏  举报