生产者消费者模型

​ 生产者是一堆线程,消费者是另一堆线程,内存缓冲区可以使用List数组队列,数据类型只需要定义一个简单的类就好。关键是如何处理多线程之间的协作。这其实也是多线程通信的一个范例。

通过wait/notify实现生产者消费者

package com.zmysna.corresponse;

import java.util.LinkedList;
/*
	消费者
*/
public class Comsumer implements Runnable {
    private LinkedList<Integer> queue;

    public Comsumer(LinkedList<Integer> queue) {
        this.queue = queue;
    }


    @Override
    public void run() {

        try {
            while (true) {
                synchronized (queue) {
                    if (queue.isEmpty()) {
                        queue.notifyAll();
                        queue.wait();
                    }
                    else System.out.println(Thread.currentThread().getName() + "消费了" + queue.removeFirst());
                }
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
package com.zmysna.corresponse;

import java.util.LinkedList;
import java.util.Random;

/*
    生产者
 */
public class Producer implements Runnable {
    private LinkedList<Integer> queue;
    private int length;

    public Producer(LinkedList<Integer> queue, int length) {
        this.queue = queue;
        this.length = length;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Random rd = new Random();
                synchronized (queue) {
                    if (queue.size() == length) {
                        queue.notifyAll();
                        queue.wait();
                    }
                    int data = rd.nextInt(100);
                    System.out.println(Thread.currentThread().getName() + "开始生产数字" + data);
                    queue.addLast(data);
                    Thread.sleep(1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
package com.zmysna.corresponse;

import java.util.LinkedList;

public class Test01 {
    public static void main(String[] args) {
        LinkedList<Integer> queue = new LinkedList<>();
        Producer producer01 = new Producer(queue,1);
        Comsumer comsumer01 = new Comsumer(queue);
        new Thread(producer01, "生产者01:").start();
        new Thread(comsumer01, "消费者03:").start();
        new Thread(comsumer01, "消费者04:").start();

    }
}

通过await/signal实现生产者消费者

package com.zmysna.corresponse;

import java.util.LinkedList;
import java.util.Random;

/*
    生产者
 */
public class LockProducer implements Runnable {
    private LinkedList<Integer> queue;
    private int length;

    public LockProducer(LinkedList<Integer> queue, int length) {
        this.queue = queue;
        this.length = length;
    }

    @Override
    public void run() {
        try {
            while (true) {
                Random rd = new Random();
                Test02.lock.lock();
                    if (queue.size() == length) {
                        Test02.isEmpty.signalAll();
                        Test02.isFull.await();
                    }
                    int data = rd.nextInt(100);
                    System.out.println(Thread.currentThread().getName() + "开始生产数字" + data);
                    queue.addLast(data);
                    Thread.sleep(1000);
                Test02.lock.unlock();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
package com.zmysna.corresponse;

import java.util.LinkedList;
/*
    消费者
 */
public class LockConsumer implements Runnable {
    private LinkedList<Integer> queue;

    public LockConsumer(LinkedList<Integer> queue) {
        this.queue = queue;
    }
    
    @Override
    public void run() {
        try {
            while (true) {
                 Test02.lock.lock();
                    if (queue.isEmpty()) {
                        Test02.isFull.signalAll();
                        Test02.isEmpty.await();
                    }
                    else System.out.println(Thread.currentThread().getName() + "消费了" + queue.removeFirst());
                Test02.lock.unlock();
                Thread.sleep(2000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
package com.zmysna.corresponse;

import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test02 {
    static Lock lock = new ReentrantLock();
    static Condition isFull = lock.newCondition();
    static Condition isEmpty = lock.newCondition();
    public static void main(String[] args) {
        LinkedList<Integer> queue = new LinkedList<>();
        LockProducer producer01 = new LockProducer(queue,2);
        LockConsumer comsumer01 = new LockConsumer(queue);
        new Thread(producer01, "生产者01:").start();
        new Thread(comsumer01, "消费者03:").start();
        new Thread(comsumer01, "消费者04:").start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值