Display indeterminate ProgressBar during heavy computation logic execution

I want to show an indeterminate ProgressBar on click of a checkbox in OptionGroup to prevent user interaction until some heavy computation task is performed. I followed the logic specified in
https://vaadin.com/docs/-/part/framework/components/components-progressbar.html
. On the valueChange listener of the checkbox I run a thread. The body of the run method of the thread is as follows.

[code]
getMyComponent().getUI().access(new Runnable() {
@Override public void run() {
showWaitIndicator();
}
});

// Some heavy computation logic

hideWaitIndicator();
[/code]Here showWaitIndicator() method displays the ProgressBar and hideWaitIndicator() method hides it. When I run the code, it happens that sometimes the showWaitIndicator() is executed only after the heavy computation logic and hideWaitIndicator() method are executed resulting in ProgressBar displayed forever. How can I make sure that the logic in the code shown above is executed only in the expected order.

Note: I posted the same question in stackoverflow yesterday - http://stackoverflow.com/questions/40853582/vaadin-send-response-from-heavy-computation-logic but didn’t get any satisfied answer.

You also need to use a Runnable and UI.access for hideWaitInidicator(). Runnables passed to UI.access are (sometimes) run asynchronously, but they are guaranteed to be run in a consistent order (technically, they are immediately added to a threadsafe queue, and that queue is then asynchronously purged).

Another thing to look out for is to make sure you have enabled push for the UI or that you’re using use UI.setPollInterval. Without either of those, it might be that hideWaitIndicator() is actually run after showWaitIndicator(), but the browser never sends any message to the server that would make it discover that the progress bar has been removed.