生产者和消费者的Java多线程实现(改造版)

队列的设计

public class Buffer<T> {

    private T[] data;
    private int size;
    private boolean empty;
    private int reader;
    private int writer;

    public boolean isEmpty() {
        return reader == writer;
    }

    public boolean isFull(){
        return (writer + 1) % size == reader;
    }

    public int size(){
        return (writer + size - reader) % size;
    }

    public Buffer(Class clazz, int size) {
        this.data = (T[]) Array.newInstance(clazz, size + 1);
        this.size = size + 1;
        this.reader = 0;
        this.writer = 0;
        this.empty = true;
    }

    public boolean push(T ele){
        if(isFull()){
            return false;
        }
        data[writer] = ele;
        writer = (++writer) % size;
        return true;
    }

    public T pop(){
        if(isEmpty()){
            return null;
        }
        T val = data[reader];
        reader = (++reader) % size;
        return val;
    }

    public synchronized void produce(T ele){
        while (isFull()){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        this.push(ele);
        System.out.println(Thread.currentThread().getName() + ":" + ele);
        this.notify();
    }

    public synchronized void consume(){
        while (isEmpty()){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        T data = this.pop();
        System.out.println(Thread.currentThread().getName() + ":" + data);
        this.notify();
    }
}

 

生产者设计

public class Producer implements Runnable{

    private final Buffer<Integer> buffer;

    public Producer(Buffer<Integer> buffer) {
        this.buffer = buffer;
    }

    @Override
    public void run() {
        try {
            Random rand = new Random();
            while (true){
                int random = rand.nextInt(100);
                buffer.produce(random);
            }
        }catch (Exception e) {
            System.err.println("hit Exception" + e);
        }
    }
}

 

消费者设计

public class Consumer implements Runnable{

    private final Buffer<Integer> buffer;

    public Consumer(Buffer<Integer> buffer) {
        this.buffer = buffer;
    }

    @Override
    public void run() {
        try {
            while (true){
                Thread.sleep(3000);
                buffer.consume();
            }
        }catch (Exception e) {
            System.err.println("hit Exception" + e);
        }
    }
}

 

改动点:将对Buffer的消费和生产的同步操作放到Buffer内部。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值