/** * Retrieves and removes the head of the queue represented by this deque * (in other words, the first element of this deque), or returns * <tt>null</tt> if this deque is empty. * * <p>This method is equivalent to {@link #pollFirst}. * * @return the head of the queue represented by this deque, or * <tt>null</tt> if this deque is empty */ public E poll() { return pollFirst(); }
private int runTasks(final ArrayDeque<Runnable> taskQueue) { int workCount = 0; Runnable runnable; while (null != (runnable = taskQueue.pollFirst())) { runnable.run(); workCount += 1; } return workCount; }
/** {@inheritDoc} */ @Override public E pollFirst() { E e = super.pollFirst(); items.remove(e); return e; } }
/** * @throws NoSuchElementException {@inheritDoc} */ public E removeFirst() { E x = pollFirst(); if (x == null) throw new NoSuchElementException(); return x; }
private void tryEmitAndDrainAllFailedBuffers() { for (FailedBuffer failedBuffer; (failedBuffer = failedBuffers.pollFirst()) != null; ) { sendWithRetries(failedBuffer.buffer, failedBuffer.length, failedBuffer.eventCount, false); approximateFailedBuffersCount.decrementAndGet(); } }
@Override public SubtitleInputBuffer dequeueInputBuffer() throws SubtitleDecoderException { Assertions.checkState(dequeuedInputBuffer == null); if (availableInputBuffers.isEmpty()) { return null; } dequeuedInputBuffer = availableInputBuffers.pollFirst(); return dequeuedInputBuffer; }
/** * Get the next queued response, waiting if necessary. The returned response <em>must</em> be closed. * * @return the queued response * @throws InterruptedException if the thread was interrupted while waiting */ public Response getResponse() throws InterruptedException { final ArrayDeque<Response> responses = this.responses; synchronized (responses) { if (cancelled) { throw new IllegalStateException("Waiting on cancelled response"); } while (responses.isEmpty()) { responses.wait(); } return responses.pollFirst(); } }
private void tryEmitOneFailedBuffer() { FailedBuffer failedBuffer = failedBuffers.peekFirst(); if (failedBuffer != null) { if (sendWithRetries(failedBuffer.buffer, failedBuffer.length, failedBuffer.eventCount, false)) { // Remove from the queue of failed buffer. failedBuffers.pollFirst(); approximateFailedBuffersCount.decrementAndGet(); // Don't add the failed buffer back to the buffersToReuse queue here, because in a situation when we were not // able to emit events for a while we don't have a way to discard buffers that were used to accumulate events // during that period, if they are added back to buffersToReuse. For instance it may result in having 100 // buffers in rotation even if we need just 2. } } }
private void sendAllValid(final ChannelHandlerContext ctx, final PerChannel perChannel, final long now) { // write operations need synchronization synchronized (perChannel) { ToSend newToSend = perChannel.messagesQueue.pollFirst(); for (; newToSend != null; newToSend = perChannel.messagesQueue.pollFirst()) { if (newToSend.relativeTimeAction <= now) { long size = newToSend.size; trafficCounter.bytesRealWriteFlowControl(size); perChannel.queueSize -= size; queuesSize.addAndGet(-size); ctx.write(newToSend.toSend, newToSend.promise); perChannel.lastWriteTimestamp = now; } else { perChannel.messagesQueue.addFirst(newToSend); break; } } if (perChannel.messagesQueue.isEmpty()) { releaseWriteSuspended(ctx); } } ctx.flush(); } }
@Nullable @Override public Object poll() { return store.pollFirst(); }
private void sendAllValid(final ChannelHandlerContext ctx, final long now) { // write order control synchronized (this) { ToSend newToSend = messagesQueue.pollFirst(); for (; newToSend != null; newToSend = messagesQueue.pollFirst()) { if (newToSend.relativeTimeAction <= now) { long size = calculateSize(newToSend.toSend); trafficCounter.bytesRealWriteFlowControl(size); queueSize -= size; ctx.write(newToSend.toSend, newToSend.promise); } else { messagesQueue.addFirst(newToSend); break; } } if (messagesQueue.isEmpty()) { releaseWriteSuspended(ctx); } } ctx.flush(); }
private void sendAllValid(final ChannelHandlerContext ctx, final PerChannel perChannel, final long now) { // write operations need synchronization synchronized (perChannel) { ToSend newToSend = perChannel.messagesQueue.pollFirst(); for (; newToSend != null; newToSend = perChannel.messagesQueue.pollFirst()) { if (newToSend.relativeTimeAction <= now) { long size = newToSend.size; trafficCounter.bytesRealWriteFlowControl(size); perChannel.channelTrafficCounter.bytesRealWriteFlowControl(size); perChannel.queueSize -= size; queuesSize.addAndGet(-size); ctx.write(newToSend.toSend, newToSend.promise); perChannel.lastWriteTimestamp = now; } else { perChannel.messagesQueue.addFirst(newToSend); break; } } if (perChannel.messagesQueue.isEmpty()) { releaseWriteSuspended(ctx); } } ctx.flush(); }
public void close() { synchronized (lock) { safeClose(sourceChannel); ByteBuffer buffer; while ((buffer = queue.pollFirst()) != null) { ByteBufferPool.free(buffer); } } }
private void completeBufferedSequence() throws IOException { LOG.debug("{}: Finished feeding back buffered data.", inputGate.getOwningTaskName()); currentBuffered.cleanup(); currentBuffered = queuedBuffered.pollFirst(); if (currentBuffered != null) { currentBuffered.open(); numQueuedBytes -= currentBuffered.size(); } }
private void sendAllValid(final ChannelHandlerContext ctx, final long now) { // write order control synchronized (this) { ToSend newToSend = messagesQueue.pollFirst(); for (; newToSend != null; newToSend = messagesQueue.pollFirst()) { if (newToSend.relativeTimeAction <= now) { long size = calculateSize(newToSend.toSend); trafficCounter.bytesRealWriteFlowControl(size); queueSize -= size; ctx.write(newToSend.toSend, newToSend.promise); } else { messagesQueue.addFirst(newToSend); break; } } if (messagesQueue.isEmpty()) { releaseWriteSuspended(ctx); } } ctx.flush(); }
private void rewriteToBlockSize(int targetBlockBits) { assert targetBlockBits <= maxBitsPerBlock; // We copy over data blocks to an output with one-larger block bit size. // We also discard references to blocks as we're copying to allow GC to // clean up partial results in case of memory pressure. ByteBuffersDataOutput cloned = new ByteBuffersDataOutput(targetBlockBits, targetBlockBits, blockAllocate, NO_REUSE); ByteBuffer block; while ((block = blocks.pollFirst()) != null) { block.flip(); cloned.writeBytes(block); if (blockReuse != NO_REUSE) { blockReuse.accept(block); } } assert blocks.isEmpty(); this.blockBits = targetBlockBits; blocks.addAll(cloned.blocks); }
/** * @param type Message type. * @param customMsg Custom message. * @return {@code True} if should not process message. */ private boolean skipMessage(int type, @Nullable DiscoveryCustomMessage customMsg) { if (type == EVT_DISCOVERY_CUSTOM_EVT) { assert customMsg != null && customMsg.id() != null : customMsg; if (rcvdCustomMsgs.contains(customMsg.id())) { if (log.isDebugEnabled()) log.debug("Received duplicated custom message, will ignore [msg=" + customMsg + "]"); return true; } rcvdCustomMsgs.addLast(customMsg.id()); while (rcvdCustomMsgs.size() > DISCOVERY_HISTORY_SIZE) rcvdCustomMsgs.pollFirst(); } return false; }
private int sendQueuedResponses() { int workCount = 0; if (!controlPublication.isConnected()) { state = State.INACTIVE; } else { if (!queuedResponses.isEmpty()) { if (sendFirst(queuedResponses)) { queuedResponses.pollFirst(); activityDeadlineMs = Aeron.NULL_VALUE; workCount++; } else if (activityDeadlineMs == Aeron.NULL_VALUE) { activityDeadlineMs = epochClock.time() + connectTimeoutMs; } else if (hasGoneInactive()) { state = State.INACTIVE; } } } return workCount; }
SubtitleOutputBuffer outputBuffer = availableOutputBuffers.pollFirst(); outputBuffer.addFlag(C.BUFFER_FLAG_END_OF_STREAM); releaseInputBuffer(inputBuffer); SubtitleOutputBuffer outputBuffer = availableOutputBuffers.pollFirst(); outputBuffer.setContent(inputBuffer.timeUs, subtitle, Format.OFFSET_SAMPLE_RELATIVE); releaseInputBuffer(inputBuffer);
/** * @param rcvCnt Number of messages received by remote node. */ public void ackReceived(long rcvCnt) { if (log.isDebugEnabled()) log.debug("Handle acknowledgment [acked=" + acked + ", rcvCnt=" + rcvCnt + ", msgReqs=" + msgReqs.size() + ']'); while (acked < rcvCnt) { SessionWriteRequest req = msgReqs.pollFirst(); assert req != null : "Missed message [rcvCnt=" + rcvCnt + ", acked=" + acked + ", desc=" + this + ']'; if (req.ackClosure() != null) req.ackClosure().apply(null); req.onAckReceived(); acked++; } }