LinkedBlockingQueue常用方法
LinkedBlockingQueue是 java.util.concurrent 包下的类,是一个阻塞的线程安全的队列,底层采用链表实现。遵循FIFO(先进先出)。
LinkedBlockingQueue添加元素的方法有三个:add、put、offer。且都是向队列尾部添加元素。
LinkedBlockingQueue移除元素的方法有三个:poll、remove、take。且都是从队列中取出头部元素并从队列中删除。
LinkedBlockingQueue可以在构造函数的参数中指定大小,若没有指定大小,则默认大小为Integer.MAX_VALUE。LinkedBlockingQueue的大小不可为null。
add、offer、put的区别
add
/** * Inserts the specified element into this queue if it is possible to do * so immediately without violating capacity restrictions, returning * {@code true} upon success and throwing an * {@code IllegalStateException} if no space is currently available. * When using a capacity-restricted queue, it is generally preferable to * use {@link #offer(Object) offer}. * * @param e the element to add * @return {@code true} (as specified by {@link Collection#add}) * @throws IllegalStateException if the element cannot be added at this * time due to capacity restrictions * @throws ClassCastException if the class of the specified element * prevents it from being added to this queue * @throws NullPointerException if the specified element is null * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this queue */ boolean add(E e);
offer
/** * Inserts the specified element into this queue if it is possible to do * so immediately without violating capacity restrictions, returning * {@code true} upon success and {@code false} if no space is currently * available. When using a capacity-restricted queue, this method is * generally preferable to {@link #add}, which can fail to insert an * element only by throwing an exception. * * @param e the element to add * @return {@code true} if the element was added to this queue, else * {@code false} * @throws ClassCastException if the class of the specified element * prevents it from being added to this queue * @throws NullPointerException if the specified element is null * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this queue */ boolean offer(E e);
put
/** * Inserts the specified element into this queue, waiting if necessary * for space to become available. * * @param e the element to add * @throws InterruptedException if interrupted while waiting * @throws ClassCastException if the class of the specified element * prevents it from being added to this queue * @throws NullPointerException if the specified element is null * @throws IllegalArgumentException if some property of the specified * element prevents it from being added to this queue */ void put(E e) throws InterruptedException;
总结:
- add:方法在添加元素的时候,若超出了度列的长度会直接抛出异常。
- offer:方法添加元素,如果队列已满,直接返回false。
- put:方法添加元素,如果队列已满,会阻塞直到有空间可以放。
poll、remove、take的区别
poll
/** * Retrieves and removes the head of this queue, waiting up to the * specified wait time if necessary for an element to become available. * * @param timeout how long to wait before giving up, in units of * {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the * {@code timeout} parameter * @return the head of this queue, or {@code null} if the * specified waiting time elapses before an element is available * @throws InterruptedException if interrupted while waiting */ E poll(long timeout, TimeUnit unit) throws InterruptedException;
take
/** * Retrieves and removes the head of this queue, waiting if necessary * until an element becomes available. * * @return the head of this queue * @throws InterruptedException if interrupted while waiting */ E take() throws InterruptedException;
remove
/** * Removes a single instance of the specified element from this queue, * if it is present. More formally, removes an element {@code e} such * that {@code o.equals(e)}, if this queue contains one or more such * elements. * Returns {@code true} if this queue contained the specified element * (or equivalently, if this queue changed as a result of the call). * * @param o element to be removed from this queue, if present * @return {@code true} if this queue changed as a result of the call * @throws ClassCastException if the class of the specified element * is incompatible with this queue * (<a href="../Collection.html#optional-restrictions">optional</a>) * @throws NullPointerException if the specified element is null * (<a href="../Collection.html#optional-restrictions">optional</a>) */ boolean remove(Object o);
总结:
- poll: 若队列为空,返回null。
- take:若队列为空,发生阻塞,等待到有元素。
- remove::若队列为空,抛出NoSuchElementException异常。