码迷,mamicode.com
首页 > 数据库 > 详细

基于链表的队列LinkedBlockingQueue学习

时间:2016-06-11 13:18:53      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

LinkedBlockingQueue为先进先出队列

1.链表中的节点,next为后继节点
    static class Node<E> {
        E item;
        Node<E> next;
        Node(E x) { item = x; }
  }

2.三种构造方法a.容量为最大值 b.容量为指定大小 c.容量为最大值,使用Collection c初始化队列
  public LinkedBlockingQueue() {this(Integer.MAX_VALUE);}
  public LinkedBlockingQueue(int capacity) {......}
  public LinkedBlockingQueue(Collection<? extends E> c) {......}

3.常用方法add(),offer(),put(),poll(),peek(),clear()
    add():调用父类AbstractQueue中的方法,父类调用LinkedBlockingQueue.offer(e)方法
    public boolean add(E e) {
           if (offer(e))
               return true;
           else
               throw new IllegalStateException("Queue full");
        }
        offer():等待指定时间插入一个元素,如果时间到元素还没有添加进去就返回false。
     Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for
     space to become available.
            public boolean offer(E e) {......}
            public boolean offer(E e, long timeout, TimeUnit unit){......}
        put():Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.
            public void put(E e){......}
        poll():Removes a node from head of queue.
            public E poll() {......}
            public E poll(long timeout, TimeUnit unit) {......}  
            如果队列数量等于0,等待指定时间后返回null
        peek():查看队列的头,并不移除该元素
            public E peek() {return first.item;}
        clear():Atomically removes all of the elements from this queue.
            public void clear() {......}

 

基于链表的队列LinkedBlockingQueue学习

标签:

原文地址:http://www.cnblogs.com/wanshoushou/p/5575161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!