執行緒池之阻塞佇列
阿新 • • 發佈:2018-12-10
1. ArrayBlockingQueue
存
add: 拋異常
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
put : 阻塞
while (count == items.length)
notFull.await();
offer : false
public boolean offer(E e, long timeout, TimeUnit unit) 在指定等待時間內阻塞
while (count == items.length) { if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); }
取
poll : 沒有返回null
return (count == 0) ? null : dequeue();
take : 阻塞
while (count == 0)
notEmpty.await();
peek : 返回第1個元素 沒有返回null
drainTo(Collection<? super E> c, int maxElements)