为什么从接口说起
我们知道JAVA的设计都是从接口开始的,只有先定义了行为规范,后续实现的时候才能做到心中有数。可以说如果明白了接口方法的含义,即使不看其一系列的实现,你也能很好的使用ArrayBlockingQueue等等实现,相反,若先扎进实现之中,则容易陷进细节出不来。可以说BlockingQueue是所有同步队列的纲。
方法
boolean add(E e);
boolean offer(E e);
void put(E e) throws InterruptedException;
boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;
boolean remove(Object o);
E poll(long timeout, TimeUnit unit) throws InterruptedException;
E take() throws InterruptedException;
E poll(); //这个方法在BlockingQueue的父接口Queue里
E peek();//这个方法在BlockingQueue的父接口Queue里
E element();//这个方法在BlockingQueue的父接口Queue里
单词
我们先看看英语单词,挺重要的。
offer 【提供,供给】 就和企业给你了offer,但你可以不去一样,offer并没有强制的意思,所以相比add方法,offer就不会在队列满的时候抛异常。 也不会像put那样不插入就一直等待。
put 【放入】 有强制的意思。
poll 【投票,选举,轮询】 相比take,他也少了一层强制的意思,所以当队列为空的时候,它只返回null,而不像take那样要一直等到队列不为空。
take 拿出,有强制的意思。
peek 窥视,偷瞄
element 组成,成分。
上述这两个单词都跟拿出没任何关系,他俩就是看看队列有没有元素。
问题:如果队列为空,peek,element其中要有一个抛出队列为空的异常,你选择哪个?
答案在最后,相信聪明的你能猜到。
插入系列
boolean add(E e);
boolean offer(E e);
void put(E e) throws InterruptedException;
boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;
插入系列有四个方法,如果先看实现的话,非常容易一头雾水,为什么一个插入需要这么多方法,不就是一个先进先出的队列吗?我们一个一个说。
首先,
boolean add(E e); add在不违反容器容量限制的情况下,立即插入一个元素,成功返回true,否则返回false。另外,若满队列则抛出IllegalStateException。 我们大概能猜出这个方法的实现是
if(不满) {插入元素; return true;}
else {throw new IllegalStateException()}
boolean offer(E e); 不违反容器容量限制的情况下,立即插入一个元素,成功返回true,否则返回false。与add的区别就在于满队列的时候是否抛出异常。大概实现如下
if(不满) {插入元素; return true;}
else {return false;}
void put(E e) throws InterruptedException; 插入指定元素入队,如果队列满,则等待直到插入为止。因为该方法要求必须插入,所以方法就不再返回true false了。同时引入了等待操作,所以要抛出InterruptedException。大概实现如下。
while(true){
if(不满) {插入元素; }
else {wait()} //这个方法的关注点在如何唤醒
}
boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;
该方法重写了offer方法,引入了等待时间。如果队列为满,可以等待一定时间,如果队列为空,则插入,当时间到了之后队列仍然满的话则返回false。等待期间自然有可能抛出异常。
start = now(); //开始时间
end = start + timeout; //终止时间
boolean insert = false;//插入标志
while(true) {
if(不满) {插入元素; return true;}
else {wait(剩余时间)} //这个方法的关注点在如何唤醒
if(end - now() <= 0 ) return false; //时间到了返回false
}
移出系列
boolean remove(Object o);
E poll(long timeout, TimeUnit unit) throws InterruptedException;
E take() throws InterruptedException;
E poll(); //这个方法在BlockingQueue的父接口Queue里
这里就不写大概实现了,相信大家都有考虑。看到InterruptedException就是这个方法跟等待有关。
boolean remove(Object o); 存在则移除指定元素,这个方法不属于先进先出队列的方法。
E poll(long timeout, TimeUnit unit) throws InterruptedException; 等待一定的时间,如果在这期间内队列不为空,则拿出头节点。
E take() throws InterruptedException; 等待直到队列不为空,拿出头节点。
E poll(); 拿出头节点,无则返回null。
最后父接口Queue的两个方法。,这两个方法都不拿出,只是观察头节点有没有,peek没有的话返回null,element没有的话则抛NoSuchElementException异常。
/**
* Retrieves, but does not remove, the head of this queue,
* or returns {@code null} if this queue is empty.
* @return the head of this queue, or {@code null} if this queue is empty
*/
E peek();
/**
* Retrieves, but does not remove, the head of this queue. This method
* differs from {@link #peek peek} only in that it throws an exception
* if this queue is empty.
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
E element();
相信大家对这些方法的区别有了清晰的认识,再看实现类的时候会轻松很多,至少不会有这个方法究竟有神马用的感觉。
开头问题答案:
element会抛出异常。peek是偷窥,你去偷看别人洗澡,发现没人洗澡,难道偷看的人还有胆子抛异常吗。