手写一个生产者消费者

1. 方式一

采用synchronizedwait/notify的同步方式

public class BlockQueue<T>{
     private int index;
     private final int MAX_SIZE = 50;
     private final LinkedHashMap<Integer, T> linkedHashMap = new LinkedHashMap<>();
     
	 public synchronized void put(T t) throws InterruptedException {
            if (linkedHashMap.size() == MAX_SIZE) {
                wait();
            } else {
                notifyAll();
            }
            linkedHashMap.put(index, t);
            index++;
            if (index > MAX_SIZE) index = index % MAX_SIZE;
        }

        public synchronized T get() throws InterruptedException {
            if (linkedHashMap.size() == 0) {
                wait();
            } else {
                notifyAll();
            }
            T t = null;
            Iterator<Map.Entry<Integer, T>> iterator = linkedHashMap.entrySet().iterator();
            if (iterator.hasNext()) {
                t = linkedHashMap.remove(iterator.next().getKey());
            }
            return t;
        }
}

2. 方式二

采用lockcondition.await/signal方式

public class BlockQueue2<T>{
	 private final Lock lock = new ReentrantLock();
     private final Condition condition = lock.newCondition();
     private int index;
     private final int MAX_SIZE = 50;
     private final LinkedHashMap<Integer, T> linkedHashMap = new LinkedHashMap<>();

	 public void put(T t) {
            lock.lock();
            try {
                if (linkedHashMap.size() == MAX_SIZE) {
                    condition.await();
                } else {
                    condition.signal();
                }
                linkedHashMap.put(index, t);
                System.out.println("生产:" + t);
                index++;
                if (index == MAX_SIZE) index = index % MAX_SIZE;
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }

        public T get() {
            lock.lock();
            T t = null;
            try {
                if (linkedHashMap.size() == 0) {
                    condition.await();
                } else {
                    condition.signal();
                }
                Iterator<Map.Entry<Integer, T>> iterator = linkedHashMap.entrySet().iterator();
                if (iterator.hasNext()) {
                    Map.Entry<Integer, T> next = iterator.next();
                    t = next.getValue();
                    linkedHashMap.remove(next.getKey());
                    System.out.println("消费:" + t);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            return t;
        }
}

3. 方式三

使用阻塞队列BlockingDeque

public class BlockingQueue3<T> {
	 private BlockingDeque<T> blockingDeque = new LinkedBlockingDeque<>();
	
	  public void put(T msg) {
	      blockingDeque.push(msg);
	  }
	
	  public T get() throws InterruptedException {
	      return blockingDeque.take();
	  }
}

4. 测试程序

public static void main([String[] args) {
     BlockingQueue<Integer> queue = new BlockingQueue<>();
     new Thread(() -> {
         for (int i = 0; i < 60; i++) {
             queue.put(i);
         }
     }).start();
     new Thread(() -> {
         for (; ; ) {
             queue.get();
         }
     }).start();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值