summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2013-05-29 10:43:07 +0300
committerVaadin Code Review <review@vaadin.com>2013-06-05 09:09:35 +0000
commitb1ab27a8fe2e06ab4d16f5b6f4ff107d694e19a6 (patch)
treecf4c7f90c4dcb6c9569f4668cb401755b26f0508
parent8d655f617fe09b65982b259ba8d402e3f1d6bf24 (diff)
downloadvaadin-framework-b1ab27a8fe2e06ab4d16f5b6f4ff107d694e19a6.tar.gz
vaadin-framework-b1ab27a8fe2e06ab4d16f5b6f4ff107d694e19a6.zip
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
-rw-r--r--uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java33
-rw-r--r--uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java2
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