Java多线程之生产者消费者模型

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * 消费者:
 * 1. 消费商品
 * 2. 从容器中取出商品
 * 3. 如果容器为空,通知生产者生产
 */

class Customer implements Runnable{
    private final Queue<Goods> queue;

    public Customer(Queue<Goods> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
    while (true){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (this.queue){
            if(this.queue.isEmpty()){
                System.out.println(Thread.currentThread().getName()+"容器已空,请继续生产");
                this.queue.notifyAll();
            }else{
                Goods goods = this.queue.poll();
                if(goods != null){
                    System.out.println(Thread.currentThread().getName()+"正在消费商品"+goods);
                }
            }
        }
    }
    }
}

/**
 * 商品
 */
class Goods{
    private final String id;
    private final String name;

    public Goods(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

/**
 *  生产者:
 * 1. 生产商品
 * 2. 将生产的商品添加到容器
 * 3. 如果容器已经满了,等待消费(等通知)
 */
class Producer implements Runnable{
    private final Queue<Goods> queue;
    private final Integer maxCapacity = 10;
    private final AtomicInteger id = new AtomicInteger(0);
    public Producer(Queue<Goods> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
    while(true){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }synchronized(this.queue) {
            if (this.queue.size() == maxCapacity) {
                //wait
                System.out.println(Thread.currentThread().getName() + " 容器满了  等待消费");
                try {
                    this.queue.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                Goods goods = new Goods(
                        String.valueOf(id.getAndIncrement()),
                        "商品"
                );
                System.out.println(Thread.currentThread().getName() + " 生产商品 " + goods);
                this.queue.add(goods);
            }
        }
    }
    }
    
}

public class text_Subject {
    public static void main(String[] args) {
        //容器  file ,db , kafka, redis, mq , zookeeper
        final Queue<Goods> queue = new LinkedList<>();
        //生产者的业务类
        final Runnable produce = new Producer(queue);
        //消费者的业务类
        final Runnable customer = new Customer(queue);

        //生产者线程
        for (int i = 0; i < 5; i++) {
            new Thread(produce, "Thread-Produce-" + i).start();
        }
        //消费者线程
        for (int i = 0; i < 8; i++) {
            new Thread(customer, "Thread-Customer-" + i).start();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值