java线程之join方法

来源:互联网 发布:进口数据查询 编辑:程序博客网 时间:2024/05/18 17:25

直接看下面的程序:

package cn.edu.ustc.thread;public class TestJoin {public static void main(String[] args){MyThread2 t2 = new MyThread2("thomas");t2.start();try {t2.join();} catch (InterruptedException e) {e.printStackTrace();}for(int i=1;i<10;i++){System.out.println("I am main thread");}}}class MyThread2 extends Thread{MyThread2(String s){super(s);}public void run(){for(int i = 1;i<=10;i++){System.out.println("i am" +" "+ getName());}}}

输出结果:

i am thomas
i am thomas
i am thomas
i am thomas
i am thomas
i am thomas
i am thomas
i am thomas
i am thomas
i am thomas
I am main thread
I am main thread
I am main thread
I am main thread
I am main thread
I am main thread
I am main thread
I am main thread
I am main thread

说明:

join方法的作用是将这个线程合并到当前的线程一块执行,相当于方法调用,等这个线程执行完之后,继续执行main

主线程。



原创粉丝点击