【2022初春】【笔记】生产者消费者问题

生产者消费者模式,是通过一个容器来解决生产者和消费者之间的强耦合关系。生产者和消费者之间不直接通信,而是通过阻塞队列进行通信。
锁在函数/while循环外,是对象锁,一个生产者/消费者持续工作。锁在while内,用户随机获取线程。

import java.util.LinkedList;
import java.util.Queue;

public class pac {
    Queue<Integer> queue = new LinkedList<>();
    final int maxsize = 10;
    class Producer extends Thread{
        @Override
        public void run(){producer();}
        public void producer(){
            while(true){
                synchronized (queue){
                    while(queue.size()==maxsize){
                        queue.notifyAll();
                        System.out.println(this.getName()+"当前队列满");
                        try {
                            queue.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    queue.add(1);
                    queue.notify();
                    System.out.println(this.getName()+"生产者生产一条任务,当前队列长度为" + queue.size());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    class Consumer extends Thread{
        @Override
        public void run(){consumer();}
        public void consumer(){
            while(true){
                synchronized (queue){
                    while(queue.size()==0){
                        queue.notifyAll();
                        System.out.println(this.getName()+"当前队列为空");
                        try {
                            queue.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    queue.poll();
                    queue.notify();
                    System.out.println(this.getName()+"消费者消费一条任务,当前队列长度为" + queue.size());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }
    public static void main(String[] args){
        pac p = new pac();
        Producer producer = p.new Producer();
        Producer producer1 = p.new Producer();
        Consumer consumer = p.new Consumer();
        Consumer consumer1 = p.new Consumer();
        producer.setName("s1");
        producer1.setName("s2");
        consumer.setName("c1");
        consumer1.setName("c2");
        producer.start();
        producer1.start();
        consumer.start();
        consumer1.start();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值