public class BlockQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        //addRemoveTest();
offerPollDemo();
        //putTake();
    }

    private static void addRemoveTest() {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        System.out.println(queue.add("1"));
        System.out.println(queue.add("2"));
        System.out.println(queue.add("3"));
        System.out.println(queue.add("4")); //队列满,直接抛出异常

        System.out.println(queue.remove());
        System.out.println(queue.remove());
        System.out.println(queue.remove());
        System.out.println(queue.remove()); //队列空,直接抛出异常

    }

    private static void offerPollDemo() throws InterruptedException {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        System.out.println(queue.offer("1"));
        System.out.println(queue.offer("2"));
        System.out.println(queue.offer("3"));
        System.out.println(queue.offer("4")); *//队列满,返回fal*se

        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll()); //队列空,返回null

        System.out.println(queue.offer("1",1000, TimeUnit.MILLISECONDS));
        System.out.println(queue.offer("2",1000, TimeUnit.MILLISECONDS));
        System.out.println(queue.offer("3",1000, TimeUnit.MILLISECONDS));
        System.out.println(queue.offer("4",1000, TimeUnit.MILLISECONDS)); //队列满快,1s时间内还没有空余空间则放弃
    }

    private static void putTake() throws InterruptedException {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        queue.put("1");
        queue.put("2");
        queue.put("3");
        //queue.put("4"); //队列满了,阻塞

        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take());
        System.out.println(queue.take()); //队列空了 阻塞
    }

}

SynchronousQueue<String> queue = new SynchronousQueue<>();
<p>new Thread(()-> {
try {
queue.put("1"); // 没有size,一存就阻塞
System.out.println("输入1");
queue.put("2"); // 没有size,一存就阻塞
System.out.println("输入2");
queue.put("3"); // 没有size,一存就阻塞
System.out.println("输入3");
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();</p>
<p>new Thread(()-> {
try {
System.out.println(queue.take());
System.out.println(queue.take());
System.out.println(queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();