@Override public E next() { try { return next; } finally { next = poll(); } } };
public QueueConsumer<E> consumer() { return new QueueConsumer<>(tail.get()); } }
@Override public Iterator<E> iterator() { return new Iterator<E>() { private E next = poll(); @Override public boolean hasNext() { return next != null; } @Override public E next() { try { return next; } finally { next = poll(); } } }; } }
public int drain(List<E> sink) { int drained = 0; for (E item; (item = poll()) != null; sink.add(item), drained++); return drained; }
private void cycle(WorkerThread t) throws InterruptedException { final AsyncRecord<K, V> rec = queueConsumer.poll(); if (rec != null) { sendNow(rec.record, rec.callback); yields = 0; } else if (yields++ < MAX_YIELDS) { Thread.yield(); } else { Thread.sleep(QUEUE_BACKOFF_MILLIS); } }
private void cycle(WorkerThread thread) throws InterruptedException { final Message m = consumer.poll(); if (m != null) { for (MessageHandler handler : handlers) { handler.onMessage(context, m); } } else if (yields++ < maxYields) { Thread.yield(); } else { // resetting yields here appears counterintuitive (it makes more sense to reset it on a hit than a miss), // however, this technique avoids writing to an instance field from a hotspot, markedly improving performance // at the expense of (1) prematurely sleeping on the next miss and (2) yielding after a sleep yields = 0; Thread.sleep(POLL_BACKOFF_MILLIS); } }
@Override public void cycle(WorkerThread thread) throws InterruptedException { final Message m = consumer.poll(); if (m != null) { if (debugMessageCounts != 0) { final long consumed = this.consumed.getAndIncrement(); if (consumed % debugMessageCounts == 0) { logLine.accept(String.format("groupId=%s, consumed=%,d", groupId, consumed)); } } handler.onMessage(context, m); } else if (yields++ < maxYields) { Thread.yield(); } else { // resetting yields here appears counterintuitive (it makes more sense to reset it on a hit than a miss); // however, this technique avoids writing to an instance field from a hotspot, markedly improving performance // at the expense of (1) prematurely sleeping on the next miss and (2) yielding after a sleep yields = 0; Thread.sleep(POLL_BACKOFF_MILLIS); } } }
private void publisherCycle(WorkerThread t) throws InterruptedException { List<AsyncRecord> recs = null; for (;;) { final AsyncRecord rec = queueConsumer.poll(); if (rec != null) { if (recs == null) { recs = new ArrayList<>(); yields = 0; } recs.add(rec); if (recs.size() == MAX_BATCH_SIZE) { sendNow(recs); return; } } else { if (recs != null) { sendNow(recs); } else if (yields++ < PUBLISH_MAX_YIELDS) { Thread.yield(); } else { Thread.sleep(PUBLISH_BACKOFF_MILLIS); } return; } } }