会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132837个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/智能电话本项目实战 1981楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1983楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 1985楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 1986楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/智能电话本项目实战 1988楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1989楼

package com.bjsxt.homework.Thread;

public class JoinThread {
    public static void main(String[] args) {
        System.out.println("main线程开始");
        Thread  t1 = new Thread(new A());

        t1.start();
        //new B().start();
        for(int i =0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+"正在运行"+i);

            //i=3时,main被挂起,直到A执行完。
        if (i == 3) {

            try {
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
        System.out.println("main线程结束");
    }
}
 class A implements Runnable{
    Thread t = new B();

    @Override
    public void run() {
        t.run();
        for(int i =0;i<10;i++){
        System.out.println(Thread.currentThread().getName()+"正在运行"+i);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //i=5时,A被挂起,直到B执行完。
        if(i==5){
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }}

}
 class B extends Thread{
     @Override
     public void run() {
         for(int i =0;i<10;i++){
             System.out.println(Thread.currentThread().getName()+"正在运行!!!"+i);
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
     }

     }
 }


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1990楼

com.bjsxt.homework.Thread;

JoinThread {
    main(String[] args) {
        System..println();
        Thread  t1 = Thread(A());

        t1.start();
        (i =;i<;i++){
            System..println(Thread.().getName()++i);

            (i == ) {

            {
                t1.join();
            } (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
        System..println();
    }
}
 A Runnable{
    Thread = B();

    run() {
        .run();
        (i =;i<;i++){
        System..println(Thread.().getName()++i);
        {
            Thread.();
        } (InterruptedException e) {
            e.printStackTrace();
        }

        (i==){
            {
                .join();
            } (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }}

}
 B Thread{
     run() {
         (i =;i<;i++){
             System..println(Thread.().getName()++i);
             {
                 Thread.();
             } (InterruptedException e) {
                 e.printStackTrace();
             }
     }

     }
 }


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1991楼

代码:

com.bjsxt.homework.Thread;

JoinThread {
    main(String[] args) {
        System..println();
        Thread  t1 = Thread(A());

        t1.start();
        (i =;i<;i++){
            System..println(Thread.().getName()++i);

            (i == ) {

            {
                t1.join();
            } (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
        System..println();
    }
}
 A Runnable{
    Thread = B();

    run() {
        .run();
        (i =;i<;i++){
        System..println(Thread.().getName()++i);
        {
            Thread.();
        } (InterruptedException e) {
            e.printStackTrace();
        }

        (i==){
            {
                .join();
            } (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }}

}
 B Thread{
     run() {
         (i =;i<;i++){
             System..println(Thread.().getName()++i);
             {
                 Thread.();
             } (InterruptedException e) {
                 e.printStackTrace();
             }
     }

     }
 }


运行结果:
main线程开始
main正在运行0
main正在运行1
main正在运行2
main正在运行3
Thread-1正在运行!!!0
Thread-1正在运行!!!1
Thread-1正在运行!!!2
Thread-1正在运行!!!3
Thread-1正在运行!!!4
Thread-1正在运行!!!5
Thread-1正在运行!!!6
Thread-1正在运行!!!7
Thread-1正在运行!!!8
Thread-1正在运行!!!9
Thread-1正在运行0
Thread-1正在运行1
Thread-1正在运行2
Thread-1正在运行3
Thread-1正在运行4
Thread-1正在运行5
Thread-1正在运行6
Thread-1正在运行7
Thread-1正在运行8
Thread-1正在运行9
main正在运行4
main正在运行5
main正在运行6
main正在运行7
main正在运行8
main正在运行9
main线程结束

Process finished with exit code 0

请问为什么线程中不能创建线程呢???只有一个Thread1

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1992楼

/**
 * 定义馒头类
 */
class mantou2{
    private int id ;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public mantou2(int id) {
        this.id = id;
    }
}

/**
 * 定义缓冲区
 */

class buffer{
    private mantou2[] mt = new mantou2[10];
    private int index ;

    /**
     * 放馒头
     */
    public synchronized void push(mantou2 mantou2){
        while (this.index == this.mt.length){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        notify();
        this.mt[index] = mantou2 ;
        index++ ;
    }

    /**
     * 取馒头
     */
    public synchronized mantou2 pop(){
        while (this.index == 0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        notify();
        this.index--;
        return this.mt[index] ;
    }
}

/**
 * 取馒头线程
 */
class qumantou extends Thread{
    buffer buffer = new buffer();
    public qumantou(com.itbz.buffer buffer) {
        this.buffer = buffer;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10;i++){
            buffer.pop() ;
            System.out.println("取馒头" + i);
        }
    }
}
/**
 * 放馒头线程
 */
class cunmantou extends Thread{
    buffer buffer = new buffer();
    public cunmantou(com.itbz.buffer buffer) {
        this.buffer = buffer;
    }

    @Override
    public void run() {
       for (int i = 0; i < 10;i++){
           System.out.println("存馒头" + i);
           mantou2 mantou2 = new mantou2(i);
           buffer.push(mantou2);
       }
    }
}

public class ProductThread2 {
    public static void main(String[] args) {
        buffer buffer = new buffer();
        cunmantou cunmantou = new cunmantou(buffer);
        qumantou qumantou = new qumantou(buffer);
        cunmantou.start();
        qumantou.start();
    }
}

问题 在主方法中声明两个存取馒头的时候,要对存取馒头两个类进行增加有参构造方法而不能使用默认无参构造方法才能正常,如果使用无参构造方法时,程序会阻塞?为什么?这块不太理解

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1993楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 1994楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 1995楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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