From b1ab27a8fe2e06ab4d16f5b6f4ff107d694e19a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Wed, 29 May 2013 10:43:07 +0300 Subject: [PATCH] Update broadcaster tutorial code * Don't need to start a thread for broadcasting with the new UI.access * Add @Push to the UI * Use CopyOnWriteArrayList instead of synchronized methods Change-Id: I63fad6977c1a3e504fc60445c3147b5a241c3132 --- .../broadcastingmessages/Broadcaster.java | 33 ++++--------------- .../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 listeners = new ArrayList(); + private static final List listeners = new CopyOnWriteArrayList(); - 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 getListeners() { - List listenerCopy = new ArrayList(); - 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 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 -- 2.39.5