码迷,mamicode.com
首页 > 编程语言 > 详细

加入线程

时间:2017-02-17 18:53:48      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:结束   ace   暂停   run   art   system   blog   rac   ted   

加入线程 join(), 当前线程暂停, 等待指定的线程执行结束后, 当前线程再继续

join(int), 可以等待指定的毫秒之后继续

 

final Thread t1 = new Thread() {
    public void run() {
        for(int i = 0; i < 50; i++) {
            System.out.println(getName() + "...aaaaaaaaaaaaaaaaaaaaaa");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
};

Thread t2 = new Thread() {
    public void run() {
        for(int i = 0; i < 50; i++) {
            if(i == 2) {
                try {
                    //t1.join();                        //插队,加入
                    t1.join(30);                        //加入,有固定的时间,过了固定时间,继续交替执行
                    Thread.sleep(10);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
            }
            System.out.println(getName() + "...bb");

        }
    }
};

t1.start();
t2.start();

 

加入线程

标签:结束   ace   暂停   run   art   system   blog   rac   ted   

原文地址:http://www.cnblogs.com/loaderman/p/6411146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!