From 09279b8ac0a2f1bee170fcfa56f273aec5ce0ffb Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 11 Sep 2018 21:53:48 +0200 Subject: [PATCH] Add AbstractBlockingQueueWithShutdown.putAll(Collection) --- .../util/ArrayBlockingQueueWithShutdown.java | 50 ++++++++++++++----- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/ArrayBlockingQueueWithShutdown.java b/smack-core/src/main/java/org/jivesoftware/smack/util/ArrayBlockingQueueWithShutdown.java index 752c1c5fc..ab95510df 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/ArrayBlockingQueueWithShutdown.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/ArrayBlockingQueueWithShutdown.java @@ -58,10 +58,16 @@ public class ArrayBlockingQueueWithShutdown extends AbstractQueue implemen } private void insert(E e) { + insert(e, true); + } + + private void insert(E e, boolean signalNotEmpty) { items[putIndex] = e; putIndex = inc(putIndex); count++; - notEmpty.signal(); + if (signalNotEmpty) { + notEmpty.signal(); + } } private E extract() { @@ -226,6 +232,22 @@ public class ArrayBlockingQueueWithShutdown extends AbstractQueue implemen } } + private void putInternal(E e, boolean signalNotEmpty) throws InterruptedException { + assert lock.isHeldByCurrentThread(); + + while (isFull()) { + try { + notFull.await(); + checkNotShutdown(); + } + catch (InterruptedException ie) { + notFull.signal(); + throw ie; + } + } + insert(e, signalNotEmpty); + } + /** * Inserts the specified element into this queue, waiting if necessary * for space to become available. @@ -246,23 +268,27 @@ public class ArrayBlockingQueueWithShutdown extends AbstractQueue implemen lock.lockInterruptibly(); try { - while (isFull()) { - try { - notFull.await(); - checkNotShutdown(); - } - catch (InterruptedException ie) { - notFull.signal(); - throw ie; - } - } - insert(e); + putInternal(e, true); } finally { lock.unlock(); } } + public void putAll(Collection elements) throws InterruptedException { + checkNotNull(elements); + lock.lockInterruptibly(); + + try { + for (E element : elements) { + putInternal(element, false); + } + } finally { + notEmpty.signalAll(); + lock.unlock(); + } + } + public enum TryPutResult { /** * The method was unable to acquire the queue lock.