]> source.dussan.org Git - vaadin-framework.git/commitdiff
Update broadcaster tutorial code
authorLeif Åstrand <leif@vaadin.com>
Wed, 29 May 2013 07:43:07 +0000 (10:43 +0300)
committerVaadin Code Review <review@vaadin.com>
Wed, 5 Jun 2013 09:09:35 +0000 (09:09 +0000)
* 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

uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java
uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java

index 57ad0d97baf30247b90479683af6eb168972d289..78d0af62834a058879d514a26e8ac3227f0d1b70 100644 (file)
 
 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 {
index 06ead20db39f1a0920f7aef3ab1df41e6188b539..68b5925f48064d1281c0d1dc9f840ecb9f8d4953 100644 (file)
@@ -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