龙须面

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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次之后就不再执行,等到子线程执行完成后再继续执行!

posted on 2012-06-27 09:02  木子小黑  阅读(345)  评论(0编辑  收藏  举报