阻塞队列:如果BlockQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会被唤醒.同样,如果BlockingQueue是满的,任何试图往里存东西的操作也会被阻断进入等待状态,直到BlockingQueue里有空间才会被唤醒继续操作.
1)offer(E e):表示如果可能的话,将e加到BlockingQueue里,即如果BlockingQueue可以容纳,则返回true,否则返回false.
2)add(E e):内部重载offer(E e)
3)put(E e):内部重载offer(E e)
4)poll(time):取走BlockingQueue里排在首位的对象,若不能立即取出,则可以等time参数规定的时间,取不到时返回null
5)take():取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态直到Blocking有新的对象被加入为止
6)peek(E e):获取但不移除此队列的头;如果此队列为空,则返回 null。
offer();
public boolean offer(E e) { if (e == null) throw new NullPointerException(); //锁保证事务安全 final ReentrantLock lock = this.lock; lock.lock(); int n, cap; Object[] array; while ((n = size) >= (cap = (array = queue).length)) //底层为数组自增长 tryGrow(array, cap); try { Comparator<? super E> cmp = comparator; if (cmp == null) //根据字典顺序移动底层数组 siftUpComparable(n, e, array); else //根据比较器移动底层数组 siftUpUsingComparator(n, e, array, cmp); size = n + 1; //释放信号量表示队列不为空; notEmpty.signal(); } finally { lock.unlock(); } return true; }poll();
public E poll() { //线程安全控制 final ReentrantLock lock = this.lock; lock.lock(); try { //出队操作 return dequeue(); } finally { lock.unlock(); } }
public E take() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); E result; try { while ( (result = dequeue()) == null) //空队列保持等待 notEmpty.await(); } finally { lock.unlock(); } return result; }
public E peek() { final ReentrantLock lock = this.lock; lock.lock(); try { return (size == 0) ? null : (E) queue[0]; } finally { lock.unlock(); } }
DelayQueue 案例demo : http://www.cnblogs.com/jobs/archive/2007/04/27/730255.html
http://blog.csdn.net/flyingpig4/article/details/6043128
BlockingQueue: http://www.cnblogs.com/likwo/archive/2010/07/01/1769199.html
PriorityBlockingQueue :http://blog.csdn.net/forever_crying/article/details/8071014
更多多线程精彩内容请继续关注我的博客:http://blog.csdn.net/caicongyang