三种消费者生产者模式写法

package ConcurrentSource;/*
@author 黄大宁Rhinos
@date 2019/5/12 - 22:33
**/

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

public class CustomerAndProducer01 {
    private Queue<Integer> queue;
    private int maxSize;
    CustomerAndProducer01(int maxSize){
        this.queue = new LinkedList<Integer>();
        this.maxSize = maxSize;
    }
    class Customer implements Runnable{
        public void use(){
            synchronized (queue){
                while(queue.size()==0){
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                queue.poll();
                System.out.println(Thread.currentThread().getName() + "消费后还有"+queue.size());
                queue.notifyAll();
            }
        }
        @Override
        public void run() {
            for(int i=0;i<5;i++){
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                use();
            }
        }
    }
    class Producer implements Runnable{
        public void produce(){
            synchronized (queue){
                while(queue.size()==maxSize){
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                queue.offer(1);
                System.out.println(Thread.currentThread().getName() +"生产后还有"+queue.size());
                queue.notifyAll();
            }
        }
        @Override
        public void run() {
            for(int i=0;i<5;i++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                produce();
            }

        }
    }

    public static void main(String[] args) {
        CustomerAndProducer01 cp = new CustomerAndProducer01(10);
        for(int i=0;i<10;i++){
            new Thread(cp.new Customer()).start();
        }
        for(int i=0;i<5;i++){
            new Thread(cp.new Producer()).start();
        }
    }
}


package ConcurrentSource;/*
@author 黄大宁Rhinos
@date 2019/5/12 - 23:18
**/

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

public class CustomerAndProducer02 {
    private Queue<Integer> queue;
    private int maxSize;
    private Lock lock;
    private Condition isFull;
    private Condition isEmpty;
    CustomerAndProducer02(int maxSize){
        this.queue = new LinkedList<Integer>();
        this.maxSize = maxSize;
        this.lock = new ReentrantLock();
        isEmpty = lock.newCondition();
        isFull = lock.newCondition();
    }
    class Customer implements Runnable{
        public void use(){
            lock.lock();
            try{
                while(queue.size()==0){
                    try {
                        isEmpty.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                queue.poll();
                System.out.println(Thread.currentThread().getName() + "消费后还有"+queue.size());
                isFull.signalAll();
            }finally {
                lock.unlock();
            }
        }
        @Override
        public void run() {
            for(int i=0;i<5;i++){
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                use();
            }
        }
    }
    class Producer implements Runnable{
        public void produce(){
            lock.lock();
            try{
                while(queue.size()==maxSize){
                    try {
                        isFull.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                queue.offer(1);
                System.out.println(Thread.currentThread().getName() + "消费后还有"+queue.size());
                isEmpty.signalAll();
            }finally {
                lock.unlock();
            }
        }
        @Override
        public void run() {
            for(int i=0;i<5;i++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                produce();
            }

        }
    }

    public static void main(String[] args) {
        CustomerAndProducer02 cp = new CustomerAndProducer02(10);
        for(int i=0;i<10;i++){
            new Thread(cp.new Customer()).start();
        }
        for(int i=0;i<5;i++){
            new Thread(cp.new Producer()).start();
        }
    }
}

package ConcurrentSource;/*
@author 黄大宁Rhinos
@date 2019/5/12 - 23:27
**/

import java.util.concurrent.LinkedBlockingQueue;


public class CustomerAndProducer03 {
    private LinkedBlockingQueue queue;
    private int maxSize;
    CustomerAndProducer03(int maxSize){
        this.queue = new LinkedBlockingQueue(maxSize);
        this.maxSize = maxSize;
    }

    class Customer implements Runnable{
        @Override
        public void run() {
            for(int i=0;i<5;i++){
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    queue.take();
                    System.out.println(Thread.currentThread().getName() + "消费后还有"+queue.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    class Producer implements Runnable{
        @Override
        public void run() {
            for(int i=0;i<5;i++){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    queue.put(1);
                    System.out.println(Thread.currentThread().getName() + "生产后还有"+queue.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    public static void main(String[] args) {
        CustomerAndProducer03 cp = new CustomerAndProducer03(10);
        for(int i=0;i<10;i++){
            new Thread(cp.new Customer()).start();
        }
        for(int i=0;i<5;i++){
            new Thread(cp.new Producer()).start();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值