I’m looking for a design pattern or approach for the following scenario. I wish to kick off two separate background threads for data retrieval from different sources. I then want one method (on the UI thread) to be called once both background threads have completed their work. As the data from the two sources must be combined to be useful, I must wait until both have finished retrieving before manipulating the data. How can I achieve this on the Android platform?
Share
Edit: My first version has been bothering me, and I didn’t like the necessary added boolean with it, so here’s another version. Call it with
thisfromonPostExecuteof each added task.I’ll keep the older one up also, since they both work, but I think the above is much cleaner. Now to go tidy up one of my earlier projects.
It’s easily extendable to however many tasks you have, and there’s no need for a thread to handle the threads. Just call the method at the end of each task. If it’s not the last one done, nothing happens.
Edit: Good point, but I don’t think it needs to be atomic. Since both AsyncTasks’
onPostExecutemethods run on the UI thread, they’ll be called one after the other.