import java.util.NoSuchElementException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Example {
public static void main(String[] Args) {
BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>(2);
System.out.println(blockingQueue.offer("offer添加元素"));
System.out.println(blockingQueue.add("add添加元素"));
try {
blockingQueue.add("队列满了,会曝出异常");
}catch (IllegalStateException e){
System.out.println(e.getMessage());
}
System.out.println(blockingQueue.offer("队列满了,返回false"));
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.poll());
try{
blockingQueue.remove();
}catch (NoSuchElementException e){
System.out.println(e.getMessage());
}
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.offer("offer添加元素"));
System.out.println(blockingQueue.add("add添加元素"));
System.out.println(blockingQueue.element());
System.out.println(blockingQueue.peek());
blockingQueue.clear();
System.out.println(blockingQueue.peek());
try{
blockingQueue.element();
}catch (NoSuchElementException e){
System.out.println(e.getMessage());
}
}
}