1、 创建GuardedQueue类
    public class GuardedQueue {
    private final Queue sourceList;
    public GuardedQueue() {
    this.sourceList = new LinkedBlockingQueue<>();
    }
    public synchronized Integer get() {
    while (sourceList.isEmpty()) {
    try {
    System.out.println(“——wait”);
    wait(); // <—- 如果队列为null,等待
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.println(“——“+sourceList.peek());
    return sourceList.peek();
    }
    public synchronized void put(Integer e) {
    sourceList.add(e);
    System.out.println(“++”+sourceList.peek());
    notifyAll(); //<—- 通知,继续执行 }
    }
    2、测试一下
    public class App {
    public static void main(String[] args) {
    GuardedQueue guardedQueue = new GuardedQueue();
    ExecutorService executorService = Executors.newFixedThreadPool(3);
    executorService.execute(() -> {
    guardedQueue.get();
    }
    );
    Thread.sleep(2000);
    executorService.execute(() -> {
    guardedQueue.put(20);
    }
    );
    executorService.shutdown();
    executorService.awaitTermination(30, TimeUnit.SECONDS);
    }
    }
    3.结果
    —-wait
    ++20
    —-20