JAVA 笔记[12.2][多线程之 join]

join

join 方法有三种重载方式
方法 作用
join() 等待被join 的线程执行完毕
join(long millis) 等待被join 的线程执行 millis 毫秒, millis毫秒后被join 的线程未执行完,则不再等待

join 线程

JoinLearn.java

public class JoinLearn extends Thread{
    public JoinLearn(String name){
        super(name);
    }

    public void run(){
        int i = 0; 
        for(; i<10; i++){
            System.out.println( "线程:"+getName()+"__"+i );
        }
    }

    public static void main(String[] args) throws Exception {
        JoinLearn j1 = new JoinLearn("childA");
        j1.start();
        JoinLearn j2 = new JoinLearn("childB");
        for(int i=0; i<30; i++){
            if(i==10){
                j2.start();
            }

            if(i==20){
                j2.join();
            }

            System.out.println("线程:main__"+i);

        }

    }

}

运行

线程:main__17
线程:main__18
线程:main__19
线程:childB__1
线程:childB__2
线程:childA__8
线程:childA__9
线程:childB__3
线程:childB__4
线程:childB__5
线程:childB__6
线程:childB__7
线程:childB__8
线程:childB__9
线程:main__20
线程:main__21
线程:main__22
线程:main__23
线程:main__24
线程:main__25
线程:main__26
线程:main__27
线程:main__28
线程:main__29

你可能感兴趣的:(JAVA 笔记[12.2][多线程之 join])