UI.getCurrent().access() thread


I am using UI.getCurrent().access(
new Runnable() {

public void run() {
// create data for my dojo graphs
}) method to create graphs and updating data on them. But if the user clicks on refresh while this thread in collecting data, I need a mechanism to kill this thread outside this method and start fresh with the user input.

Any pointers to kill this runnable thread before completion and impact on the GUI refresh?

Thanks in advance.

You cannot do that with Runnable, but with Thread you can. Thread for example has method destroy() for this purpose. You should check difference between these two and then decide whether Thread fits your application better.

Some discussion about Thread vs. Runnable here


I found this the vaadin documentation

accesspublic java.util.concurrent.Future<java.lang.Void> access(java.lang.Runnable runnable)
Provides exclusive access to this UI from outside a request handling thread.The given runnable is executed while holding the session lock to ensure exclusive access to this UI. If the session is not locked, the lock will be acquired and the runnable is run right away. If the session is currently locked, the runnable will be run before that lock is released.

RPC handlers for components inside this UI do not need to use this method as the session is automatically locked by the framework during RPC handling.

Please note that the runnable might be invoked on a different thread or later on the current thread, which means that custom thread locals might not have the expected values when the runnable is executed. Inheritable values in CurrentInstance will have the same values as when this method was invoked. getCurrent(), VaadinSession.getCurrent() and VaadinService.getCurrent() are set according to this UI before executing the runnable. Non-inheritable CurrentInstance values including VaadinService.getCurrentRequest() and VaadinService.getCurrentResponse() will not be defined.

The returned future can be used to check for task completion and to cancel the task.

Trying to collect this return object and cancel the future if I get another request from GUI.
Will see if it works and kills the runnable thread as welll.

Will keep you posted.

Yes, it is adviced that you execute only very short processes in UI.access() in order not to block the UI.

If you have back end access or something else you want to do async, do not run that in UI.access() thread directly. Split your process in smaller pieces.

Hi Tatu,

I have lot of processing to do as part of GUI launch, I am not sure what you meant by running that in ui.access() thread directly?



Sorry, I had a typo there not was missing. I recommend not to do the heavy processing in UI.access(). Use UI.access() only when you need to do update the UI. E.g. start a Thread which does the heavy processing and once results ready, update update UI using UI.access().

Br. Tatu

how to write query remove states from filters that are not in existing calls and cannot be reached by current models (active tables).