JAVA:生产者消费者模型

消费者: 

class ConsumerDemo extends Thread{
    private Queue<Integer> queue;
    private int maxSize;
    private Condition lock;
   private ReentrantLock reentrantLock;

    public ConsumerDemo(Queue<Integer> queue, Integer size) {
        this.queue = queue;
        this.maxSize = size;

    }
    public void run() {
        int i = 0;
        while (++i < 30) {
            synchronized (queue) {
                while (queue.isEmpty()) {
                    System.out.println("queue is empty, consumer thread is wait...");
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Integer remove = queue.remove();
                System.out.println("consumer value:"+remove);
                queue.notify();
            }
        }
    }
}

生产者:

class ProducerDemo extends Thread{
    private Queue<Integer> queue;
    private int maxSize;
    public ProducerDemo(Queue<Integer> queue, Integer size) {
        this.queue = queue;
        this.maxSize = size;
    }
    public void run() {
        int i = 0;
        while (++i < 30) {
            synchronized (queue) {
                while (queue.size() == maxSize) {
                    System.out.println("queue is full, product thread is wait...");
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Random random = new Random();
                int nextInt = random.nextInt(100);
                System.out.println("product value:"+nextInt);
                queue.add(nextInt);
                queue.notify();
            }
        }
    }
}

main方法:

 public static void main(String[] args) {
      
        Queue<Integer> qe = new ArrayDeque<>();
        Thread thread1 = new Thread(new ProducerDemo(qe, 1));
        thread1.start();
        Thread thread2 = new Thread(new ConsumerDemo(qe, 1));
        thread2.start();

    }

结果:

product value:45
queue is full, product thread is wait...
consumer value:45
queue is empty, consumer thread is wait...
product value:34
queue is full, product thread is wait...
consumer value:34
queue is empty, consumer thread is wait...
product value:73
queue is full, product thread is wait...
consumer value:73
queue is empty, consumer thread is wait...
product value:7
queue is full, product thread is wait...
consumer value:7
queue is empty, consumer thread is wait...
product value:9
queue is full, product thread is wait...
consumer value:9
queue is empty, consumer thread is wait...
product value:90
queue is full, product thread is wait...
consumer value:90
queue is empty, consumer thread is wait...
product value:95
queue is full, product thread is wait...
consumer value:95
queue is empty, consumer thread is wait...
product value:43
queue is full, product thread is wait...
consumer value:43
queue is empty, consumer thread is wait...
product value:63
queue is full, product thread is wait...
consumer value:63
queue is empty, consumer thread is wait...
product value:55
queue is full, product thread is wait...
consumer value:55
queue is empty, consumer thread is wait...
product value:18
queue is full, product thread is wait...
consumer value:18
queue is empty, consumer thread is wait...
product value:64
queue is full, product thread is wait...
consumer value:64
queue is empty, consumer thread is wait...
product value:60
queue is full, product thread is wait...
consumer value:60
queue is empty, consumer thread is wait...
product value:38
queue is full, product thread is wait...
consumer value:38
queue is empty, consumer thread is wait...
product value:72
queue is full, product thread is wait...
consumer value:72
queue is empty, consumer thread is wait...
product value:5
queue is full, product thread is wait...
consumer value:5
queue is empty, consumer thread is wait...
product value:38
queue is full, product thread is wait...
consumer value:38
queue is empty, consumer thread is wait...
product value:34

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值