java多线程-join插队

join
合并线程,插队线程,将此线程执行完成后,再执行其他线程,其他线程阻塞
join是一个成员方法,必须通过Thread对象调用

创新互联公司"三网合一"的企业建站思路。企业可建设拥有电脑版、微信版、手机版的企业网站。实现跨屏营销,产品发布一步更新,电脑网络+移动网络一网打尽,满足企业的营销需求!创新互联公司具备承接各种类型的成都网站设计、成都网站制作项目的能力。经过10多年的努力的开拓,为不同行业的企事业单位提供了优质的服务,并获得了客户的一致好评。

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("回家");
}
}

文章名称:java多线程-join插队
链接地址:http://lszwz.com/article/ihegog.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款