fetching data in parallel [closed]

杀马特。学长 韩版系。学妹 提交于 2019-12-13 08:24:45

问题


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.


回答1:


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, ...)


来源:https://stackoverflow.com/questions/21836059/fetching-data-in-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!