会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132850个问题
JAVA 全系列/第八阶段:SpringBoot与MybatisPlus/Spring Boot旧 6991楼
JAVA 全系列/第十七阶段:微信与支付宝在线支付/微信支付 6992楼
JAVA 全系列/第八阶段:SpringBoot与MybatisPlus/Spring Boot旧 6994楼
Python 全系列/第九阶段:前端进阶-高效开发Vue框架/Vue3 6995楼
JAVA 全系列/第十四阶段:海量数据存储与查询/MyCat(旧) 6996楼
WEB前端全系列/第六阶段:音乐社区高级项目模块/音乐社区项目_蓝莓派 6997楼

500.jpg

WebDemo.zip

老师,我的登录直接就直接跳转到这了,今天一天时间都卡在这没研究出原因,麻烦帮忙查看一下

JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 6998楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 7001楼

class A implements Runnable{
    private Thread b;
    public A(Thread b){
        this.b=b;
    }

    @Override
    public void run() {
        for (int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+" A "+i);
            if (i==6){
                try {
                    b.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class B implements Runnable{

    @Override
    public void run() {
        for (int i =0;i<20;i++){
            System.out.println(Thread.currentThread().getName()+" B "+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class TestJoinThread{
    public static void main(String[] args) {
        Thread t1 = new Thread(new A(t));
        Thread t = new Thread(new B());
        t1.start();
        t.start();
        for (int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+" 主线程 "+i);
            if (i==4){
                try {
                    t1.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
Thread t1 = new Thread(new A(t));
        Thread t = new Thread(new B());

为什么写成这样报错,不都是把B给A里了吗

Thread t1 = new Thread(new B());
        Thread t = new Thread(new A(t1));

视频是这样的

还有为什么要先创建线程B的对象

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 7002楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库 7003楼
JAVA 全系列/第十阶段:百战旅游网项目/百战旅游网 7005楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637