线程池之阻塞队列

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) 

猜你喜欢

转载自blog.csdn.net/qq_29857681/article/details/82768741