dontpush updates on the client

hi,
I have one problem using vaadin and dontpush addon with separate thread.

in my scenario, on application start I am creating Application instance that Im passing to other thread then I am waiting 5 sec in that separate thread before I create anything and then I am adding popup to the application. and just after that I get

Received message from client:?repaintAll=1&sh=1050&sw=1680&cw=1680&ch=965&vw=1680&vh=965&fr=#
Painting changes
Painting changes
Exception in thread “Thread-17” java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:373)
at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:384)
at com.vaadin.ui.AbsoluteLayout.paintContent(AbsoluteLayout.java:567)
at com.vaadin.ui.AbstractComponent.paint(AbstractComponent.java:754)
at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.writeUidlResponce(AbstractCommunicationManager.java:1031)
at org.vaadin.dontpush.DontPushServlet$VaadinWebSocket.paintChanges(DontPushServlet.java:158)
at org.vaadin.dontpush.SocketCommunicationManager$1.run(SocketCommunicationManager.java:82)

so this is probably due to threads race

I read
http://vaadin.com/forum/-/message_boards/message/231271
and can you point me what

actually means.

is vaadin multi-threaded framework (so components are synchronized inside framework) or is it running in main thread of a request (so I can only synchronize code in component’s callbacks) ? I dont really know how web sockets are implemented and probably they may add another thread here… but on the other hand vaadin should live in socket’s connection thread.

I’m a bit confused how this works

thanks for hepl

If you do UI changes in listeners, no need to do anything special.

If you do UI changes in a thread you have created, use a synchronize block

synchronized(app) {
// Update any component attached to the "app" Application here
}

hi,
yep, but im not doing all UI changes in callbacks, that is a pity :frowning:

I have a background thread up there that can create any vaadin control anytime and add it to myApplication. And I pass ref to my Application to that thread. so there is no other logic that makes changes to UI.

I know this not a standard scenerio of using Vaadin :slight_smile: but maybe this is a chance to make it working

thanks

The traceback you posted matches exactly the classical “synchronized (app)” scenario. When your background thread updates the application you give it as parameter (app), put your code inside a synchronized(app){} block, and things should work much better.

ok, as long as Im synchronizing one component between my threads or inside my logic it is simple, I control it,

I am just asking whether framework itself is synchronizing any component, cos if it is and I will try to access the object from different thread synchronized on wrong instance then it is like no synchronized at all.

so in another words is vaadin as framework synchronizing anything while painting or invoking callbacks or not???

this is showing what Im talking about, I hope this will help to present the problem I have

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class ThreadsTest
{
   public static void main(String [] args)
   {
      Map<Integer, String> map = new LinkedHashMap<Integer, String>(50);
      final VaadinThread oneThread = new VaadinThread(map);
      final BackgroundThread secondThread = new BackgroundThread(map);
      oneThread.start();
      secondThread.start();
   }

   private static class VaadinThread extends Thread
   {
      private Map<Integer,String> map;
      int index;

      public VaadinThread(Map<Integer, String> map)
      {
         this.map = map;
      }

      @Override
      public void run()
      {
         while (true)
         {
            synchronized (VaadinThread.class)
            {
               final Set<Integer> keys = map.keySet();
               for(Integer key : keys)
               {
                  System.out.println("** vaadin t =" + key + " : " + map.get(key));
               }
            }
            if (index > 50)
               break;
         }
      }
   }

   private static class BackgroundThread extends Thread
   {
      private Map<Integer, String> map;
      int index;

      public BackgroundThread(Map<Integer, String> map)
      {
         this.map = map;
      }

      @Override
      public void run()
      {
         while(true)
         {
            synchronized (BackgroundThread.class)
            {
               final Set<Integer> keys = map.keySet();
               for (Integer key : keys)
               {
                  System.out.println("** second t =" + key + " : " + map.get(key));
               }
               map.put(index, "added by my background thread");
               index++;
            }
            if(index > 50)
               break;
         }
      }
   }
}

and then I get exactly the same exception

Painting changes
Exception in thread "Thread-33" java.util.ConcurrentModificationException
        at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:373)
        at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:384)
        at com.vaadin.ui.AbsoluteLayout.paintContent(AbsoluteLayout.java:567)
        at com.vaadin.ui.AbstractComponent.paint(AbstractComponent.java:754)
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.writeUidlResponce(AbstractCommunicationManager.java:1031)
        at org.vaadin.dontpush.DontPushServlet$VaadinWebSocket.paintChanges(DontPushServlet.java:158)
        at org.vaadin.dontpush.SocketCommunicationManager$1.run(SocketCommunicationManager.java:82)

and from test

Exception in thread "Thread-0" java.util.ConcurrentModificationException
	at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:373)
	at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:384)
	at com.sabre.vaadin.web.ThreadsTest$VaadinThread.run(ThreadsTest.java:42)

Yes, the framework is synchronizing requests from the UI to the application instance.

At least two previous replies told to synchronize to it, but apparently did not justify enough why the application instance.

because it is not clear to me!

you should write then in CAPITAL letters “synchronize on application instance everywhere even if you are accessing application’s children still synchronize on application instance”.