Modifying/accessing UI elements from background/worker threads

Hi!

I’m new to Vaadin and currently wondering if there is a safe way to modify/access UI elements from background/worker threads.

I understand that it is safe in case of ProgressIndicator because assigning and reading a single object reference is an atomic operation. However, more complex modifications (like inserting an item into a table) are not atomic.

For example imagine the following scenario: The client has sent a repaint request. Currently a table is repainted and Vaadin is iterating over the table item list. In that moment a worker thread inserts a new item into the table. As another thread (the thread doing the repainting) is currently iterating about the item list, this will probably result in a ConcurrentModificationException or other strange effects.

In the non-Web-Application area I’ve worked with eclipse SWT. They offer org.eclipse.swt.widgets.Display#syncExec(java.lang.Runnable) to access and modify UI elements in a multi-threading safe way. Is there a similar concept in Vaadin?

Best Regards,
Stefan

Hello, Stefan!

If I remember correctly, you can modify UI components as long as you do it in a block synchronized on your Application instance. E.g.

synchronized(getApplication()) {
    Item item = table.addItem(myItemId);
    item.getItemProperty().....
    ....
}

HTH,
/Jonatan

Hi Jonatan,

thanks for your answer. :slight_smile:

I’ve found in com.vaadin.terminal.gwt.server.CommunicationManager#handleUidlRequest(…) that there is a synchronized block on the application instance. So if all Vaadin access to the application happens in this block this approach should work.

Best regards,
Stefan