Thomas
Thomas

Reputation: 2211

Recognition of threads able to update UI components

i've searched on the web for some hints but found nothing. Maybe i was searching wrong.

My question is:

How can I determine if the current Thread im executing is eligible to modify UI components?

this is what i do now for UI update:

UI.getCurrent().access(new Runnable() {
            @Override
            public void run() {
               //modify some ui
            }
        });

In some cases i dont have to do this, it works right away in the current Thread, but sometimes not and i get an error. When i compared the Threads in the Runnable and Outside the Runnable - it was the exact same thing = http-bio-8080-exec-10

What can i do to know from the code that this is the right place to invoke UI.getCurrent().access() or do it right away?

I tried to search for differences in threads look for: VaadinService.getCurrentResponse() and VaadinService.getCurrentRequest() but i didnt see any rule or pattern.

Thanks for Your time, Thomas  

Upvotes: 1

Views: 720

Answers (1)

Thomas
Thomas

Reputation: 2211

I've got an answer from a Vaadin developer:

Johannes Dahlström wrote:

If in any doubt, use UI.access - it's never wrong.

Basically, the only place where you don't need it (or some other means to lock the session) is when you know you're in a thread that's currently handling a client request (for instance, in a component event listener). In cases you don't know whether this is the case, for instance in a method that could be called either from an event listener or a background worker thread, you should either do UI.access just to be sure, or alternatively simply access the UI directly and document in the method's contract that its caller must take care of locking.

Source: https://vaadin.com/forum#!/thread/4479235/4491237

Upvotes: 3

Related Questions