I am developing an android application in which I want to integrate data(home timeline) from multiple social medias. Making API calls and getting response from each of them is bit time consuming if done in a serial fashion. So i want a parallel environment to make API calls to facebook twitter Instagram etc. and fetch the data in minimum time.

I have used Async tasks at present for fetching data from each social media..i.e one async task will call the api of facebook for fetching data, second will call the api of twitter and so on. I call all the async tasks from UI thread. I thought that all async tasks will work simultaneously in a parallel environment.. But after running the code it seems that one async task blocks the work of others and they get called only in a serial fashion. First async task called completes the work first and the last asynctask called completes the work at last. Android version I am using for development is 4.3. I have searched about THREAD POOL EXECUTOR in android for parallel processing but i am not getting a clear idea. It would be great if anyone can help me out. Thanks in advance.

有帮助吗?

解决方案

Different versions of Android handle AsyncTasks differently. Prior to 1.6, tasks were handled serially. From 1.6 to 2.3.7 they were handled asynchronously with a thread pool. From 3.0 on they went back to handling them serially.

You can force the use of a thread pool by explicitly specifying the executer.

Instead of calling the following to execute your tasks

task.execute(param1, param2, ...)

call

task.executeOnExecuter(AsyncTask.THREAD_POOL_EXECUTER, param1, param2, ...)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top