package com.basic; import java.util.LinkedList; import java.util.concurrent.TimeUnit; /** * @program JavaBooks * @description: 写一个固定容量同步器,拥有put和get方法和getCount方法 * @author: mf * @create: 2019/12/30 22:28 */ /* 能够支持2个生产者线程以及10个消费者线程的阻塞调用 */ public class T15 { private final LinkedList lists = new LinkedList<>(); private final int MAX = 10; // 最多10个元素 private int count = 0; public synchronized void put(T t) { while (lists.size() == MAX) { // 想想为什么用while而不是用if try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lists.add(t); count++; this.notifyAll(); // 通知所有被wait挂起的线程 } public synchronized T get() { T t = null;