Java多线程之顺序执行

Java创建三个线程T1,2,3,让其按照顺序执行

利用join进行阻塞实现:

/**
 * 用Java让三个线程按照顺序执行
 */
public class ExecuteOrderly {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("T1执行完成...");
        });

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("T2执行完成...");
        });

        Thread t3 = new Thread(() -> {
            System.out.println("T3执行完成...");
        });

        t1.start();
        t1.join();

        t2.start();
        t2.join();

        t3.start();
    }
}

你可能感兴趣的:(java,jvm,开发语言)