LinkedBlockingQueue队列, add、offer、put的区别:
add:成功返回true,失败 throw new IllegalStateException("Queue full");
/**
* Inserts the specified element into this queue if it is possible to do so
* immediately without violating capacity restrictions, returning
* <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
* if no space is currently available.
*
* <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
* else throws an <tt>IllegalStateException</tt>.
*
* @param e the element to add
* @return <tt>true</tt> (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 and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
*/
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
add()测试的demo
import java.util.concurrent.LinkedBlockingQueue;
public class Demo {
public static void main(String[] args) {
LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>(2);
/*
* add:成功返回true,失败 throw new IllegalStateException("Queue full");
* offer:成功返回true,失败返回false;
* put:无返回值,若向队尾添加元素的时候发现队列已经满了会发生阻塞一直等待空间,以加入元素
*/
try {
boolean add1 = queue.add("test1");
System.out.println("add1:" + add1);
boolean add2 = queue.add("test2");
System.out.println("add2:" + add2);
boolean add3 = queue.add("test3");
System.out.println("add3:" + add3);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果:
add1:true
add2:truejava.lang.IllegalStateException: Queue full
at java.util.AbstractQueue.add(AbstractQueue.java:98)
at com.demo.Demo.main(Demo.java:18)
offer:成功返回true,失败返回false;
/**
* Inserts the specified element at the tail of this queue if it is
* possible to do so immediately without exceeding the queue's capacity,
* returning {@code true} upon success and {@code false} if this queue
* is full.
* When using a capacity-restricted queue, this method is generally
* preferable to method {@link BlockingQueue#add add}, which can fail to
* insert an element only by throwing an exception.
*
* @throws NullPointerException if the specified element is null
*/
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
final AtomicInteger count = this.count;
if (count.get() == capacity)
return false;
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
if (count.get() < capacity) {
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
}
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
return c >= 0;
}
offer()测试的demo
import java.util.concurrent.LinkedBlockingQueue;
public class Demo {
public static void main(String[] args) {
LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>(2);
/*
* add:成功返回true,失败 throw new IllegalStateException("Queue full");
* offer:成功返回true,失败返回false;
* put:无返回值,若向队尾添加元素的时候发现队列已经满了会发生阻塞一直等待空间,以加入元素
*/
boolean offer1 = queue.offer("test1");
System.out.println("offer1:" + offer1);
boolean offer2 = queue.offer("test2");
System.out.println("offer2:" + offer2);
boolean offer3 = queue.offer("test3");
System.out.println("offer3:" + offer3);
}
}
运行结果
offer1:true
offer2:true
offer3:false
put:无返回值,若向队尾添加元素的时候发现队列已经满了会发生阻塞一直等待空间,以加入元素
/**
* Inserts the specified element at the tail of this queue, waiting if
* necessary for space to become available.
*
* @throws InterruptedException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public void put(E e) throws InterruptedException {
if (e == null) throw new NullPointerException();
// Note: convention in all put/take/etc is to preset local var
// holding count negative to indicate failure unless set.
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock;
final AtomicInteger count = this.count;
putLock.lockInterruptibly();
try {
/*
* Note that count is used in wait guard even though it is
* not protected by lock. This works because count can
* only decrease at this point (all other puts are shut
* out by lock), and we (or some other waiting put) are
* signalled if it ever changes from capacity. Similarly
* for all other uses of count in other wait guards.
*/
while (count.get() == capacity) {
notFull.await();
}
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
}
LinkedBlockingQueue队列, poll、remove、take的区别:
poll: 若队列为空,返回null。
remove:若队列为空,抛出NoSuchElementException异常。
take:若队列为空,发生阻塞,等待有元素
总结:
add:成功返回true,失败 throw new IllegalStateException("Queue full");
offer:成功返回true,失败返回false;
put:无返回值,若向队尾添加元素的时候发现队列已经满了会发生阻塞一直等待空间,以加入元素