Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Background Tasks, when to use UI.getCurrent().access(....)?
When having background tasks updating the UI we usually use the UI.getCurrent().access(new Runnable()....) method.
My question is now: When do we have to use the access() method?
Fro example:
1. Create new TextField
2. Change attributes/properties of TextField
3. Add TextField of step 1 to the UI
4. Changing attributes of TextField
Currently we have put everything in the access() method, but we wonder, if we can do step 1+2 outside of the access method, since the object is not yet bound to a Vaadin UI
Yes, you can (as long no code trying to access the session gets invoked as a result). As a rule of thumb, whenever you concurrently access an object that is reachable from a VaadinSession, you have to lock the session.
As a side note, the pattern "UI.getCurrent().access(...)" is not recommended as it will likely give unexpected results in many cases. Pass the UI instance into the thread some other way (eg. capture it in a closure).
Ok, thanks.
About the UI.getCurrent(), yes, i did normaly put that in a local variable in the normal thread, so I can access it in the background thread and use that one for access(....) stuff. Otherwise the UI.getCurrent() is not always set or not the correct instance.
Yep, indeed. It's only there if the thread is manually spawned from the UI in question - if using eg. an executor service the value is most probably not what is wanted.