会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132376个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/Lambda表达式(旧) 661楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/Lambda表达式(旧) 662楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术 663楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 664楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 667楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术 668楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术 669楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器 671楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 673楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器 674楼

package com.Thread;

//生产者和消费者模式
public class produceThreadTest {
    public static void main(String[] args) {
        //创建缓冲区
        SyncStack ss = new SyncStack();
        //启动生产者线程
        produce producer = new produce(ss);
        new Thread(producer).start();
        //启动消费者线程
        new customer(ss).start();
    }

}

//定义馒头类代表数据
class Mantou{
    private int id;

    public int getId() {
        return id;
    }

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

//定义缓冲区
class SyncStack {
    //用数组表示放馒头的盒子
    private Mantou[] mantous = new Mantou[10];
    //定义索引用来操作盒子
    private int index;

    //定义放馒头的方法
    public synchronized void SetMantou(Mantou mantou) {
        //当馒头放满时,线程进行等待
        while (this.index == this.mantous.length) {
            try {
                /**
                 * wait()是Object类下的方法;
                 * wait()语法:该方法需要在synchronized语句块中使用;
                 * wait()执行后,线程会将持有的对象锁释放,并进入阻塞状态、使其他持有该对象锁的线程可以继续运行
                 */
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.mantous[this.index] = mantou;
        this.index++;
    }

    //定义取馒头的方法
    public synchronized Mantou GetMantou() {
        while (this.index == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //唤醒等待中的其中一个线程、需使用在synchronized语句块中
        this.notify();
        this.index--;
        return this.mantous[this.index];
    }
}
//定义生产者线程类
class produce implements Runnable{
        private SyncStack syncStack;
        public produce(SyncStack syncStack) {
            this.syncStack = syncStack;
        }

        public void run() {
            for(int i = 0;i < 10;i++){
                Mantou mantou = new Mantou();
                mantou.setId(i);
                this.syncStack.SetMantou(mantou);
                System.out.println("生成馒头 "+mantou.getId());
            }
        }
}
//定义消费者线程类
class customer extends Thread{
        private SyncStack syncStack;
        public customer(SyncStack syncStack) {
            this.syncStack = syncStack;
        }
        public void run() {
            for (int i = 0;i<10;i++){
                Mantou mantou = this.syncStack.GetMantou();
                System.out.println("取馒头 "+mantou.getId());
            }
        }
}

疑问:为什么我运行后,会先取馒头再生产馒头?捕获.PNG

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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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