public List<ServerMessage> takeQueue() { List<ServerMessage> copy = Collections.emptyList(); synchronized (getLock()) { // Always call listeners, even if the queue is // empty since they may add messages to the queue. if (!_listeners.isEmpty()) { for (ServerSessionListener listener : _listeners) { if (listener instanceof DeQueueListener) notifyDeQueue((DeQueueListener) listener, this, _queue); } } int size = _queue.size(); if (size > 0) { copy = new ArrayList<>(size); copy.addAll(_queue); } clearQueue(); } return copy; }