public class Thread0 { public static void main(String[] args) { Thread th = new Thread(new TestThread()); th.start(); for(int i = 1; i < 201; i++) { if(i > 100) { try{ th.join();//join方法必须要抛出异常,执行到这里就会一直等待 th线程执行完毕才接着执行,join的参数可以有参数,规定子线程运行的时间 } catch(Exception e){ e.printStackTrace(); } } } } } class TestThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub for(int i = 1; i < 301; i++) { System.out.println("TestThread:" + Thread.currentThread().getName());//使用getName可是使得结果简洁 } } }
输出结果描述:
才开始主线程和子线程交互执行,但是在主线程的中的i执行够100次之后就不再执行,等到子线程执行完成后再继续执行!