生产者消费者模型

  1. 商品类:
package com.pc;

/**
 1. Goods:生产者生产的产品类
 */
public 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. 生产者类:
package com.pc;

/**
 * 生产者:
 * 1.生产商品
 * 2.将生产好的商品添加到容器中
 * 3.若容器已满,等待消费者消费
 */
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;

public 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(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (this.queue) {
                if(queue.size() == maxCapacity) {
                    System.out.println(Thread.currentThread().getName() + "容器满了,等待消费");

                    try {
                        this.queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {

                    //获取id:
                    //UUID.randomUUID().toString()/AtomicInteger
                    //getAndIncrement():获取之后再增加
                    Goods good = new Goods(String.valueOf(id.getAndIncrement()), "A商品");
                    System.out.println(Thread.currentThread().getName() + "生产商品" + good);
                    this.queue.add(good);

                }
            }
        }

    }
}
  1. 消费者类:
package com.pc;

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

public 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(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (this.queue) {
                if(this.queue.isEmpty()) {
                    System.out.println(Thread.currentThread().getName() + "容器已空,通知生产");

                    this.queue.notifyAll();
                } else {
                    Goods good = this.queue.poll();
                    if(good != null) {
                        System.out.println(Thread.currentThread().getName() + "消费商品" + good);
                    }
                }
            }

        }
    }
}
  1. 测试类:
package com.pc;

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

public class TestProducerCustomer {

    public static void main(String[] args) {

        final Queue<Goods> queue = new LinkedList<>();
        final Runnable producer = new Producer(queue);
        final Runnable customer = new Customer(queue);

        //生产者线程
        for(int i = 0; i < 5; i++) {
            new Thread(producer, "Thread-producer-" + 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、付费专栏及课程。

余额充值