/**
* Created by Leon on 2017/5/22.
* Thread类的join方法测试
*/
public class TestJoin
{
public static void main(String[] args)
{
MyThread2 t=new MyThread2("t");
t.start();
try
{
t.join();//将子线程合并到主线程,子线程执行完主线程才执行,相当于方法调用
}
catch(InterruptedException e)
{}
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());//getname 返回线程名字
try
{
sleep(1000);
}
catch (InterruptedException e)
{
return;
}
}
}
}
输出结果: