简体   繁体   中英

how to wait multiple asynctask to finish their work?

I'm using ThreadPoolExeecutor to make 3 Asynctask to work at the same time. Every thread collected data from a RSS feed in one Arraylist and then I would like to combine these 3 Arraylist into one to updated View in main thread. The problem is I need to do this after all 3 thread finished their work, how to listen on these thread?

Use CountDownLatch . This java class provides you convenient synchronized counter. Init an object with count 3, call await() and then call countDown() on it when each task is finished. Once it is 0 the lock will be released and you can perform your task.

Create a member variable and initialize to 0, increment it on every onPostExecute. Also check if the value is 3 in all onPostExecute and if it is 3 call method to combine the arraylist.

You can call async task get function which waits for it to finish or you can hold a counter where every task will increase in its onPostExecute function. Once the counter value will be the number of async tasks, execute your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM