Assuming there may actually be data to be transferred (or you invent
some), you can use an ArrayBlockingQueue.
Otherwise, wait()/notify() is a good choice, but you need to be
careful to handle the case that the UI thread does the notify before
you get into the wait() code. The usage pattern should be:
UI thread code: {
synchronized (mySyncObject) {
<set a flag saying UI is done>
mySyncObject.notifyAll();
}
}
Background thread code: {
<post request to the UI thread>
synchronized (mySyncObject) {
while (! <flag saying UI is done>) {
try {
mySyncObject.wait();
} catch (InterruptedException ex) {
// Ignore this even if we get it.
}
}
}
}
That's a while loop, not an if, because the wait() call can in theory
be interrupted (and you have to catch the InterruptedException).
Memorize this pattern! If you're using notify/wait, it should ALWAYS
look something like this.
On Jan 6, 4:55 pm, Mark Murphy <[email protected]> wrote:
> On Thu, Jan 6, 2011 at 7:48 PM, Chris <[email protected]> wrote:
> > Mark, could you give me an example of something that would work?
> > There's a lot of tools in java.util.concurrent that could probably all
> > be used to solve this so I could use a push in the right direction.
>
> Actually, upon further review, for your case, perhaps wait()/notify()
> is the best option. You could use
> java.util.concurrent.locks.ReentrantLock, but I'm not sure whether any
> of its characteristics are useful to you.
>
> --
> Mark Murphy (a Commons
> Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> Android 2.3 Programming Books:http://commonsware.com/books
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en