Java线程中的join使用实例

JDK中解释为 Waits for this thread to die. 等待本线程结束后,下一个线程才可以运行。

实例要求:

现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行

实现代码:

package com.st.lesson02;

public class Test01 {
    //1.现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行
    public static void main(String[] args) throws InterruptedException {
        Thread th1 = new Thread01();
        Thread th2 = new Thread02();
        Thread th3 = new Thread03();
        th1.start();
        th1.join();
        System.out.println("Thread01运行结束。。。");
        th2.start();
        th2.join();
        System.out.println("Thread02运行结束。。。");
        th3.start();
        th3.join();
        System.out.println("Thread03运行结束。。。");
        System.out.println("------主函数-------");
    }
}
class Thread01 extends Thread{
    
    public void run() {
        System.out.println("Thread01...running...");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class Thread02 extends Thread{
    public void run() {
        System.out.println("Thread02...running...");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
    }
}
class Thread03 extends Thread{
    public void run() {
        System.out.println("Thread03...running...");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
    }
}

运行效果图:

 

posted @ 2017-03-02 23:10  专注、坚持  阅读(2669)  评论(0编辑  收藏  举报