线程通信_生产者消费者

博客参考相关链接,以生产消费面包为例,介绍了面包类、生产面包的类、消费面包的类以及测试类,属于信息技术领域的示例讲解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:https://www.cnblogs.com/Wenxu/p/7979023.html

举例生产消费面包

 

面包的类

public class Breads {
    
    //面包的id
    private int bid;
    //面包的个数
    private int num;
    
    //生产面包的方法(由于是demo,方便大家理解,就把synchronized关键字加到方法上面了哦)
    public synchronized void produc(){
        
        //当面包的数量不为0时,该方法处于等待状态
        if(0 != num){
            try {
                wait();//等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //当面包数量为0时,那么就开始生产面包了哦
        num  =  num +1;//数量加1
        bid = bid + 1 ;//id当然也得加1
        String threadname = Thread.currentThread().getName();
        System.out.println(threadname+"生产了一个编号为"+bid+"的面包!");
        notify();//当执行完后,去唤醒其他处于等待的线程
    }
    //消费面包的方法
    public synchronized void consume(){
        //当面包的数量为0时,该方法处于等待状态
        if(num == 0 ){
            try {
                wait();//等待
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //消费完面包了,所以面包数量降为0了
        num  =  num -1;//数量减1
        String name1 = Thread.currentThread().getName();
        System.out.println(name1+"买了一个面包编号为"+bid);
        notify();//当执行完后,去唤醒其他处于等待的线程
    }

    
    //set和get方法
    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    //有参构造
    public Breads(int bid, int num) {
        super();
        this.bid = bid;
        this.num = num;
    }

    //无参构造
    public Breads() {
        super();
        // TODO Auto-generated constructor stub
    }
}

生产面包的类 

public class Breads {
    
    //面包的id
    private int bid;
    //面包的个数
    private int num;
    
    //生产面包的方法(由于是demo,方便大家理解,就把synchronized关键字加到方法上面了哦)
    public synchronized void produc(){
        
        //当面包的数量不为0时,该方法处于等待状态
        if(0 != num){
            try {
                wait();//等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //当面包数量为0时,那么就开始生产面包了哦
        num  =  num +1;//数量加1
        bid = bid + 1 ;//id当然也得加1
        String threadname = Thread.currentThread().getName();
        System.out.println(threadname+"生产了一个编号为"+bid+"的面包!");
        notify();//当执行完后,去唤醒其他处于等待的线程
    }
    //消费面包的方法
    public synchronized void consume(){
        //当面包的数量为0时,该方法处于等待状态
        if(num == 0 ){
            try {
                wait();//等待
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //消费完面包了,所以面包数量降为0了
        num  =  num -1;//数量减1
        String name1 = Thread.currentThread().getName();
        System.out.println(name1+"买了一个面包编号为"+bid);
        notify();//当执行完后,去唤醒其他处于等待的线程
    }

    
    //set和get方法
    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    //有参构造
    public Breads(int bid, int num) {
        super();
        this.bid = bid;
        this.num = num;
    }

    //无参构造
    public Breads() {
        super();
        // TODO Auto-generated constructor stub
    }
}

消费面包的类 

public class consume extends Thread{

    //获得面包的类
    private Breads bre ;
    
    //set和get方法
    public Breads getBre() {
        return bre;
    }
    public void setBre(Breads bre) {
        this.bre = bre;
    }
    
    //继承重写run方法
    @Override
    public void run() {
        con();
    }
    
    //消费面包
    private void con() {
        // 与生产者保持一致,本系统默认循环生产20个面包(生产几个,消费几个)
        for(int i = 0;i<20;i++){
            try {
                //沉睡0.3秒(演示效果需要,可以不加)
                Thread.currentThread().sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //调用面包类里的生产面包的方法
            bre.consume();
        }
    }

    //有参构造
    public consume(Breads bre) {
        super();
        this.bre = bre;
    }

    //无参构造
    public consume() {
        super();
    }
}

测试类 

public class TestBreads {
    
    public static void main(String[] args) {
        
        //new一个面包类
        Breads bre = new Breads();
        
        //new一个生产者类
        producer proth = new producer(bre);
        //new一个消费者类
        consume conth = new consume(bre);
        
        //new一个包含消费者类的线程
        Thread t1 = new Thread(proth,"生产者");
        
        //new一个包含生产者类的线程
        Thread t2 = new Thread(conth,"消费者");
        
        //启动线程
        t1.start();
        t2.start();
        
    }
}
结果:
生产者生产了一个编号为1的面包!
消费者买了一个面包编号为1
生产者生产了一个编号为2的面包!
消费者买了一个面包编号为2
生产者生产了一个编号为3的面包!
消费者买了一个面包编号为3
...
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值