1. 程式人生 > >執行緒池之阻塞佇列

執行緒池之阻塞佇列

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)