From 164ca0423670131dd133618af1526733d3a6e31a Mon Sep 17 00:00:00 2001 From: champtar Date: Fri, 6 May 2016 20:03:52 +0200 Subject: [PATCH] Drop MessageQueue, use a LinkedBlockingQueue instead (#63) LinkedBlockingQueue are available since java 5 This is a bit more efficient as we were calling notifyAll() instead of notify() in add(), thus waking up too much This also remove 'logger.finest("Adding raw message to queue.");' Signed-off-by: Etienne CHAMPETIER --- src/main/java/org/ice4j/stack/Connector.java | 7 +- .../org/ice4j/stack/MessageProcessor.java | 9 +- .../java/org/ice4j/stack/MessageQueue.java | 118 ------------------ .../org/ice4j/stack/NetAccessManager.java | 9 +- 4 files changed, 14 insertions(+), 129 deletions(-) delete mode 100644 src/main/java/org/ice4j/stack/MessageQueue.java diff --git a/src/main/java/org/ice4j/stack/Connector.java b/src/main/java/org/ice4j/stack/Connector.java index 7a7cf15..faef277 100644 --- a/src/main/java/org/ice4j/stack/Connector.java +++ b/src/main/java/org/ice4j/stack/Connector.java @@ -20,6 +20,7 @@ package org.ice4j.stack; import java.io.*; import java.net.*; import java.nio.channels.*; +import java.util.concurrent.*; import java.util.logging.*; import org.ice4j.*; @@ -45,7 +46,7 @@ class Connector /** * The message queue is where incoming messages are added. */ - private final MessageQueue messageQueue; + private final BlockingQueue messageQueue; /** * The socket object that used by this access point to access the network. @@ -86,12 +87,12 @@ class Connector * communication. * @param remoteAddress the remote address of the socket of this * {@link Connector} if it is a TCP socket, or null if it is UDP. - * @param messageQueue the FIFO list where incoming messages should be queued + * @param messageQueue the Queue where incoming messages should be queued * @param errorHandler the instance to notify when errors occur. */ protected Connector(IceSocketWrapper socket, TransportAddress remoteAddress, - MessageQueue messageQueue, + BlockingQueue messageQueue, ErrorHandler errorHandler) { this.sock = socket; diff --git a/src/main/java/org/ice4j/stack/MessageProcessor.java b/src/main/java/org/ice4j/stack/MessageProcessor.java index 1b45ef1..a1d2da1 100644 --- a/src/main/java/org/ice4j/stack/MessageProcessor.java +++ b/src/main/java/org/ice4j/stack/MessageProcessor.java @@ -17,6 +17,7 @@ */ package org.ice4j.stack; +import java.util.concurrent.*; import java.util.logging.*; import org.ice4j.*; @@ -45,7 +46,7 @@ class MessageProcessor /** * The queue where we store incoming messages until they are collected. */ - private final MessageQueue messageQueue; + private final BlockingQueue messageQueue; /** * The listener that will be retrieving MessageEvents @@ -73,7 +74,7 @@ class MessageProcessor * * @param netAccessManager the NetAccessManager which is creating * the new instance, is going to be its owner, specifies the - * MessageQueue which is to store incoming messages, specifies the + * BlockingQueue which is to store incoming messages, specifies the * MessageEventHandler and represents the ErrorHandler to * handle exceptions in the new instance * @throws IllegalArgumentException if any of the mentioned properties of @@ -85,7 +86,7 @@ class MessageProcessor if (netAccessManager == null) throw new NullPointerException("netAccessManager"); - MessageQueue messageQueue = netAccessManager.getMessageQueue(); + BlockingQueue messageQueue = netAccessManager.getMessageQueue(); if (messageQueue == null) { @@ -125,7 +126,7 @@ class MessageProcessor try { - rawMessage = messageQueue.remove(); + rawMessage = messageQueue.take(); } catch (InterruptedException ex) { diff --git a/src/main/java/org/ice4j/stack/MessageQueue.java b/src/main/java/org/ice4j/stack/MessageQueue.java deleted file mode 100644 index d67e6e7..0000000 --- a/src/main/java/org/ice4j/stack/MessageQueue.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. - * - * Copyright @ 2015 Atlassian Pty Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.ice4j.stack; - -import java.util.*; -import java.util.logging.Logger; - -/** - * The class is used as a part of the stack's thread pooling strategy. Method - * access is synchronized and message delivery ( remove() ) is blocking in the - * case of an empty queue. - * - * @author Emil Ivov - */ -class MessageQueue -{ - /** - * The Logger used by the MessageQueue and its instances - * for logging output. - */ - private static final Logger logger - = Logger.getLogger(MessageQueue.class.getName()); - - // not sure whether Vector is the best choice here since we explicitly - //sync all methods ... review later - private final Vector queue = new Vector<>(); - - //keep a copy of the fifo size and make it accessible for concurrent - // queries. - private int size = 0; - - /** - * Create an empty MessageFIFO - */ - MessageQueue() - { - } - - /** - * Returns the number of messages currently in the queue. - * @return the number of messages in the queue. - */ - public int getSize() - { - return size; - } - - /** - * Determines whether the FIFO is currently empty. - * @return true if the FIFO is currently empty and false otherwise. - */ - public boolean isEmpty() - { - return (size == 0); - } - - /** - * Adds the specified message to the queue. - * - * @param rawMessage the message to add. - */ - public synchronized void add(RawMessage rawMessage) - { - logger.finest("Adding raw message to queue."); - queue.add(rawMessage); - size++; - - notifyAll(); - } - - /** - * Removes and returns the oldest message from the fifo. If there are - * currently no messages in the queue the method block until there is at - * least one message. - * - * - * @return the oldest message in the fifo. - * @throws java.lang.InterruptedException if an InterruptedException is - * thrown wail waiting for a new message to be added. - */ - public synchronized RawMessage remove() - throws InterruptedException - { - waitWhileEmpty(); - RawMessage rawMessage = queue.remove(0); - - size--; - return rawMessage; - } - - /** - * Blocks until there is at least one message in the queue. - * - * @throws java.lang.InterruptedException if an InterruptedException is - * thrown wail waiting for a new message to be added. - */ - public synchronized void waitWhileEmpty() - throws InterruptedException - { - while (isEmpty()) - wait(); - } -} diff --git a/src/main/java/org/ice4j/stack/NetAccessManager.java b/src/main/java/org/ice4j/stack/NetAccessManager.java index 8851da8..e5d0af2 100644 --- a/src/main/java/org/ice4j/stack/NetAccessManager.java +++ b/src/main/java/org/ice4j/stack/NetAccessManager.java @@ -20,6 +20,7 @@ package org.ice4j.stack; import java.io.*; import java.net.*; import java.util.*; +import java.util.concurrent.*; import java.util.logging.*; import org.ice4j.*; @@ -79,7 +80,7 @@ class NetAccessManager /** * A synchronized FIFO where incoming messages are stocked for processing. */ - private final MessageQueue messageQueue = new MessageQueue(); + private final BlockingQueue messageQueue = new LinkedBlockingQueue<>(); /** * A thread pool of message processors. @@ -196,13 +197,13 @@ class NetAccessManager } /** - * Gets the MessageQueue of this NetAccessManager in which + * Gets the BlockingQueue of this NetAccessManager in which * incoming messages are stocked for processing. * - * @return the MessageQueue of this NetAccessManager in + * @return the BlockingQueue of this NetAccessManager in * which incoming messages are stocked for processing */ - MessageQueue getMessageQueue() + BlockingQueue getMessageQueue() { return messageQueue; }