java简单的生产者与消费者案列

案列:当生产者生产的东西超过10个时,就等待。等待消费者消费,当没有产品的时候,消费者就等待,唤醒生产者,让生产者继续生产。

代码块:
package cn.TeacherXu;

import java.util.concurrent.LinkedBlockingQueue;

public class Test0 {
    public static void main(String[] args) {
        LinkedBlockingQueue<Apple2> queue = new LinkedBlockingQueue<>();
        Producer2 p = new Producer2(queue);
        new Thread(p,"生产者1").start();
        new Thread(p,"生产者2").start();
        new Thread(p,"生产者3").start();

        Consumer2 c = new Consumer2(queue);
        new Thread(c,"消费者1").start();
        new Thread(c,"消费者2").start();
        new Thread(c,"消费者3").start();
    }
}

class Apple2 {
    private String name;
    private int age;

    public Apple2() {
    }

    public Apple2(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "[name=" + name + ", age=" + age + "]";
    }

}

class Producer2 implements Runnable {
    // 生产者,你要把生产下的东西放到一个队列中
    private LinkedBlockingQueue<Apple2> queue;
    // 生产者和消费者要共用一个队列,所以就要把同一个队列传递给它们

    public Producer2(LinkedBlockingQueue<Apple2> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (queue) {
                while (queue.size() > 10) {
                    try {
                        // 如果生产者的产品大于10个那么就该该生产者就该唤醒消费者来消费
                        queue.notifyAll();
                        // 生产者然后等待
                        queue.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                // 如果小于10个那么生产者就该继续生产产品

                Apple2 a = new Apple2("APPLE", 500);
                queue.add(a);
                System.out.println(Thread.currentThread().getName() + "生产" + a.getName());
                // 生产出来就该叫消费者来消费量
                queue.notifyAll();
            }
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

class Consumer2 implements Runnable {
    private LinkedBlockingQueue<Apple2> queue;

    public Consumer2(LinkedBlockingQueue<Apple2> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (queue) {

                while (queue.size() == 0) {
                    // 当产品没了的时候就该等待生产了
                    try {
                        // 在等待之前叫醒生产者
                        queue.notifyAll();
                        queue.wait();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                try {
                    Apple2 a = queue.take();
                    System.out.println(Thread.currentThread().getName() + "消费了"+a.getName());
                    queue.notifyAll();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值