批量消费(调用不删除唯一标记API) 来自 https://github.com/google/guava/blob/master/guava/src/com/google/common/collect/Queues.java @param q @param buffer @param numElements @param timeout @param unit @return @throws InterruptedException
(UniqueBlockingQueue<E> q,Collection<? super E> buffer,int numElements,long timeout,TimeUnit unit)
| 159 | * @throws InterruptedException |
| 160 | */ |
| 161 | @CanIgnoreReturnValue |
| 162 | @GwtIncompatible // BlockingQueue |
| 163 | @SuppressWarnings("GoodTime") // should accept a java.time.Duration |
| 164 | private <E> int drain(UniqueBlockingQueue<E> q,Collection<? super E> buffer,int numElements,long timeout,TimeUnit unit) |
| 165 | throws InterruptedException { |
| 166 | Preconditions.checkNotNull(buffer); |
| 167 | /* |
| 168 | * This code performs one System.nanoTime() more than necessary, and in return, the time to |
| 169 | * execute Queue#drainTo is not added *on top* of waiting for the timeout (which could make |
| 170 | * the timeout arbitrarily inaccurate, given a queue that is slow to drain). |
| 171 | */ |
| 172 | long deadline = System.nanoTime() + unit.toNanos(timeout); |
| 173 | int added = 0; |
| 174 | while (threadActivity && added < numElements) { |
| 175 | // we could rely solely on #poll, but #drainTo might be more efficient when there are multiple |
| 176 | // elements already available (e.g. LinkedBlockingQueue#drainTo locks only once) |
| 177 | added += q.drainTo2(buffer, numElements - added); |
| 178 | if (added < numElements) { // not enough elements immediately available; will have to poll |
| 179 | E e = q.poll2(deadline - System.nanoTime(), TimeUnit.NANOSECONDS); |
| 180 | if (e == null) { |
| 181 | break; // we already waited enough, and there are no more elements in sight |
| 182 | } |
| 183 | buffer.add(e); |
| 184 | added++; |
| 185 | } |
| 186 | } |
| 187 | return added; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * 初始化数据 |