您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
        join
合并线程,插队线程,将此线程执行完成后,再执行其他线程,其他线程阻塞
join是一个成员方法,必须通过Thread对象调用
public class n {
public static void main(String[]args) throws InterruptedException
{
    Thread t =new Thread(()-> {
        for(int i=0;i<5;i++)
        {
            System.out.println("a"+i);
        }
    });
    t.start();
    for(int i=0;i<5;i++)
    {
        if(i%2==0)
        {
            t.join();//插队,此时main主线程被阻塞,插队线程执行完所有步骤再执行main
        }
        System.out.println("b"+i);
    }
}
}
例二:
public class n {
public static void main(String[]args) throws InterruptedException
{
    new Thread(new father()).start();
}
}
class father extends Thread{
public void run()
{
    System.out.println("想抽象,发现没了");
    System.out.println("让儿子买中华");
    Thread t=new Thread(new son());
    t.start(); //不行,各走各的逻辑错误,再加入join先执行完son,再执行father剩下的
    try {
        t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("接过烟");
}
}
class son extends Thread{
public void run()
{
    System.out.println("拿钱");
    System.out.println("路边玩10秒");
    for(int i=0;i<10;i++)
    {
        System.out.println(i+"秒过去了");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.out.println("去买烟");
    System.out.println("回家");
}
}
													免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。