diff options
-rw-r--r-- | uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java | 33 | ||||
-rw-r--r-- | uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java | 2 |
2 files changed, 9 insertions, 26 deletions
diff --git a/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java b/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java index 57ad0d97ba..78d0af6283 100644 --- a/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java +++ b/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java @@ -16,44 +16,25 @@ package com.vaadin.tests.minitutorials.broadcastingmessages; -import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; public class Broadcaster { - private static List<BroadcastListener> listeners = new ArrayList<BroadcastListener>(); + private static final List<BroadcastListener> listeners = new CopyOnWriteArrayList<BroadcastListener>(); - public synchronized static void register(BroadcastListener listener) { + public static void register(BroadcastListener listener) { listeners.add(listener); } - public synchronized static void unregister(BroadcastListener listener) { + public static void unregister(BroadcastListener listener) { listeners.remove(listener); } - private synchronized static List<BroadcastListener> getListeners() { - List<BroadcastListener> listenerCopy = new ArrayList<BroadcastListener>(); - listenerCopy.addAll(listeners); - return listenerCopy; - } - public static void broadcast(final String message) { - // Make a copy of the listener list while synchronized, can't be - // synchronized while firing the event or we would have to fire each - // event in a separate thread. - final List<BroadcastListener> listenerCopy = getListeners(); - - // We spawn another thread to avoid potential deadlocks with - // multiple UIs locked simultaneously - Thread eventThread = new Thread() { - @Override - public void run() { - for (BroadcastListener listener : listenerCopy) { - listener.receiveBroadcast(message); - } - } - }; - eventThread.start(); + for (BroadcastListener listener : listeners) { + listener.receiveBroadcast(message); + } } public interface BroadcastListener { diff --git a/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java b/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java index 06ead20db3..68b5925f48 100644 --- a/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java +++ b/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java @@ -1,5 +1,6 @@ package com.vaadin.tests.minitutorials.broadcastingmessages; +import com.vaadin.annotations.Push; import com.vaadin.server.VaadinRequest; import com.vaadin.tests.minitutorials.broadcastingmessages.Broadcaster.BroadcastListener; import com.vaadin.ui.Button; @@ -10,6 +11,7 @@ import com.vaadin.ui.TextArea; import com.vaadin.ui.UI; import com.vaadin.ui.VerticalLayout; +@Push public class BroadcasterUI extends UI implements BroadcastListener { @Override |