会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132358个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 106楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 107楼

微信图片_20230524144651.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 109楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 111楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 112楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 113楼

package com.Thread;

/**
 *测试缓冲区的作用
 */
public class TestBufferThread {
    public static void main(String[] args) {
      SyncStack ss=new SyncStack();
      new XiaoFei(ss).start();
        new Product(ss).start();
    }
}
/***
 * 设置一个馒头类
 */
class Mantou{
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
/***
 * 设置缓冲区
 */
class SyncStack{
    //定义存放那个馒头的盒子
    private Mantou[] mt=new Mantou[10];
    //操作馒头的索引
    private int index;

    /**
     * 放馒头
     */
    public synchronized void push(Mantou mantou){
        //判断盒子是否满
        while (this.index==this.mt.length){
            try {
                /**
                 * 语法:wait(),该方法必须要在synchronized块中调用。
                 * wait执行后,线程会将持有的对象锁释放,并进入阻塞状态,
                 * 其他需要该对象锁的线程就可以继续运行了。
                 */
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //唤醒取馒头的线程
            /**
             * 语法:该方法必须要在synchronized块中调用。
             * 该方法会唤醒处于等待状态队列中的一个线程。
             */
            this.notify();
            this.mt[this.index]=mantou;
            this.index++;
        }
    }
    /**
     * 取馒头
     */
    public synchronized Mantou pop(){
        while(this.index == 0){
            try {
                /**
                 * 语法:wait(),该方法必须要在synchronized块中调用。
                 * wait执行后,线程会将持有的对象锁释放,并进入阻塞状态,
                 * 其他需要该对象锁的线程就可以继续运行了。
                 */
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.index--;
        return this.mt[this.index];
    }

}
   /**
 * 定义生产者线程
 */
class  Product extends Thread{
       //传入缓冲区
       private SyncStack ss;
       public Product(SyncStack ss){
           this.ss=ss;
       }
       @Override
       public void run() {
           for (int i = 0; i <10 ; i++) {
               Mantou mt=new Mantou();
               mt.setId(i);
               System.out.println("生产馒头"+mt.getId());
               this.ss.push(mt);
           }
       }
   }
/**
 * 定义消费者线程
 */
class XiaoFei extends Thread{
    private SyncStack ss;
    public XiaoFei(SyncStack ss){
        this.ss = ss;
    }
    @Override
    public void run() {
        for(int i=0;i<10;i++){
            Mantou mt = this.ss.pop();
            System.out.println("消费馒头:"+mt.getId());
        }
    }
}

老师,帮我看看,只启动生产者线程,消费者线程没反应?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 114楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 116楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/函数式编程 117楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/函数式编程 118楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术 119楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 120楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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