Vaadin Thread models

Hello,

This is to confirm the different method of updating UI from a background thread at server. As described in
this
forum thread it’s clear that there are various ways to update the client UI after a long running server Thread, Refresher, ICEPush, and ServerPush etc. Another way is to use
Progressbar
to update the UI after(or along with) a long running task.

But in case of Vaadin-7, to update the UI we have to invoke such code either inside the access() method, or by explicitly invoking session.lock() and unlock(). As you can verify in case of Progressbar sample it’s not getting invoked neither inside access() method nor using lock()/unlock() methods(but it works fine for the samples). So basically I need a clarification whether this is fine and correct. In the demo code I found the following code, is the line “synchronized (UI.getCurrent())” do the same thing as the locak()/unlock() or access() method? Anybody confirm this and share ideas?

public class Worker extends Thread {
int current = 1;
public final static int MAX = 10;
@Override public void run() {
for (; current <= MAX; current++) {
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
e.printStackTrace();
}
synchronized (UI.getCurrent()) {
processed();
}
}
}
public int getCurrent() {
return current;
}
}

This example is incorrect - it seems to have been migrated from the Vaadin 6 approach without updating it for the locking mechanisms that were introduced after the first Vaadin 7 alpha/beta versions. If it is in somewhere on vaadin.com, please let us know where so someone can fix it. I have also seen other incorrect examples using ProgressBar and there is already a ticket about the incorrect example in the
Vaadin 7 version of the book
.

Using UI.access(…) is the recommended approach; explicit locking methods should only be used very carefully in some special situations that are unlikely to occur in most applications (they exist primarily for frameworks and certain very low-level add-ons).

https://github.com/ahoehma/vaadin-examples/tree/master/vaadin-progressbardialog

Thank you dear Henri and Andreas. Andreas, I got this sample, then I got confused how it works as in Vaadin6 sample without UI.access(…). So it should be changed in some places in vaadin forum and in the vaadin 7 book.

The forum is a forum to which a lot of people are writing, not a curated database of always correct answers. I don’t think any past answers should be changed, but you can post a more correct solution to the thread where you saw the bad example if you think it will be useful for people looking for an answer to the same question in the future.

The book will be updated shortly, I already talked to the person responsible for it.

Concurrent programming is hard, so reading more about it is strongly recommended if writing any more complicated concurrent programs.

Ok, I got it ! Yes, I need to implement some complex concurrent application, something like a cancellable long running task.

I just posted my code for a UIExecutor which supports cancelling background work. You might find it useful:
https://vaadin.com/forum#!/thread/4269500

-mike