views:

35

answers:

1

in my class I have an inline thread in the constructor that loads objects from a remote site:

Thread readSite = new Thread(new Runnable() {       
    public void run() { 
        site.loadStuff();
    } 
});
readSite.start();

I want to display a 'loading' message until the thread is finished. So before the above code I show a loading message.

After the above code I show the screen in which I would like to continue.

The code looks like this:

showLoadingView(); //tells the user it is waiting

Thread readSite = new Thread(new Runnable() {       
    public void run() { 
        site.loadStuff();
    } 
});
readSite.start();

showStuffView(); //works with the data retrieved from the site instance

Now, the main thread of course continues and the showStuffView() is directly executed.

I can now let the main thread wait for the readSite Thread, but then the user cannot accept the connection request ('is it ok to use airtime?') that is shown to the user (because the responsible thread is asleep I guess).

On the other side, I cannot execute the showStuffView() from the readSite Thread.

I hope you guys can explain how to wait for this thread. I looked into synchronized, but couldn't really find a nice solution.

A: 

I think this is a common problem with threads and this particular problem you can solver with boolean variable. but for general purpose i think observer pattern is good.

Vivart