From 68dfada8bc744e977cadc97dd7a8390449ef919e Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Wed, 3 Apr 2013 20:05:20 +0200 Subject: [PATCH] Moving in isEmpty and count into AbstractNodeQueue --- .../main/java/akka/dispatch/AbstractNodeQueue.java | 11 +++++++++++ akka-actor/src/main/scala/akka/dispatch/Mailbox.scala | 11 ++++------- .../util/SerializedSuspendableExecutionContext.scala | 11 +++++------ 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/akka-actor/src/main/java/akka/dispatch/AbstractNodeQueue.java b/akka-actor/src/main/java/akka/dispatch/AbstractNodeQueue.java index 26ed466069..14b543112c 100644 --- a/akka-actor/src/main/java/akka/dispatch/AbstractNodeQueue.java +++ b/akka-actor/src/main/java/akka/dispatch/AbstractNodeQueue.java @@ -31,6 +31,17 @@ public abstract class AbstractNodeQueue extends AtomicReference n = peek();n != null; n = n.next()) + ++count; + return count; + } + @SuppressWarnings("unchecked") public final T poll() { final Node next = peek(); diff --git a/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala b/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala index 15b93bc63f..559574a795 100644 --- a/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala +++ b/akka-actor/src/main/scala/akka/dispatch/Mailbox.scala @@ -353,12 +353,9 @@ class NodeMessageQueue extends AbstractNodeQueue[Envelope] with MessageQueue { final def dequeue(): Envelope = poll() - final def numberOfMessages: Int = { - @tailrec def count(node: AbstractNodeQueue.Node[Envelope], c: Int): Int = if (node eq null) c else count(node.next(), c + 1) - count(peek(), 0) - } + final def numberOfMessages: Int = count() - final def hasMessages: Boolean = peek() ne null + final def hasMessages: Boolean = !isEmpty() @tailrec final def cleanUp(owner: ActorRef, deadLetters: MessageQueue): Unit = { val envelope = dequeue() @@ -535,11 +532,11 @@ case class UnboundedMailbox() extends MailboxType { } /** - * MPSCUnboundedMailbox is a high-performance unbounded MailboxType, + * SingleConsumerOnlyUnboundedMailbox is a high-performance, multiple producer—single consumer, unbounded MailboxType, * the only drawback is that you can't have multiple consumers, * which rules out using it with BalancingDispatcher for instance. */ -case class MPSCUnboundedMailbox() extends MailboxType { +case class SingleConsumerOnlyUnboundedMailbox() extends MailboxType { def this(settings: ActorSystem.Settings, config: Config) = this() diff --git a/akka-actor/src/main/scala/akka/util/SerializedSuspendableExecutionContext.scala b/akka-actor/src/main/scala/akka/util/SerializedSuspendableExecutionContext.scala index 7ea58b5475..b6e7f4c09f 100644 --- a/akka-actor/src/main/scala/akka/util/SerializedSuspendableExecutionContext.scala +++ b/akka-actor/src/main/scala/akka/util/SerializedSuspendableExecutionContext.scala @@ -76,12 +76,11 @@ private[akka] final class SerializedSuspendableExecutionContext(throughput: Int) override final def execute(task: Runnable): Unit = try add(task) finally attach() override final def reportFailure(t: Throwable): Unit = context reportFailure t - final def size(): Int = { - @tailrec def count(node: AbstractNodeQueue.Node[Runnable], c: Int): Int = if (node eq null) c else count(node.next(), c + 1) - count(peek(), 0) - } - - final def isEmpty(): Boolean = peek() eq null + /** + * O(N) + * @return the number of Runnable's currently enqueued + */ + final def size(): Int = count() override final def toString: String = (state.get: @switch) match { case 0 ⇒ "Off"