1

In my app I use asynctask to parse xml then display the results in a recyclerview the same time the xml parsing is processing I am running a service in which I use another asynctask also the user may save an item clicking a button which will also execute another asynctask. Running all three (sometimes more) asynctasks at the same time even using threadpoolexecutor results in them waiting for each other to execute . Is there a better way to run all of them at the same time .

code

asynctask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, category, mTag);

Mohamed Allam
  • 125
  • 2
  • 11
  • `Running all three (sometimes more) asynctasks at the same time even using threadpoolexecutor results in them waiting for each other to execute` That shouldn't be the case. Are you sure you're using the ThreadPoolExecutor correctly? Can you paste the code that uses the thread pool executor? Something's not quite right here. – kha Aug 24 '15 at 07:35
  • @Kha i posted the code – Mohamed Allam Aug 24 '15 at 07:53
  • @kha Actually there is a thread limit, see this: http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible – Ilya Kogan Aug 24 '15 at 07:55
  • @IlyaKogan does this limit apply when running other threads like Runnable – Mohamed Allam Aug 24 '15 at 08:01
  • @IlyaKogan There is always a limit :) but even according to your link, it's not less than three. I suspect something else is going on. Maybe you're running more tasks than you think in quick succession? Is that a possibility? – kha Aug 24 '15 at 08:01
  • @kha I mean that the default limit is 1. – Ilya Kogan Aug 24 '15 at 08:09
  • yes , I got many lists which involve the user selecting and removing and every action would execute a new asynctask to add/remove items from database also liking ,saving .Is it even right to use all these asynctasks ? ,according to the reference asynctasks should be used for short tasks and in my case the tasks dont't take time when executed one by one ! The problem comes when the asynctasks wait for the longest one (the parsing) in order to execute . – Mohamed Allam Aug 24 '15 at 08:14
  • @IlyaKogan The default limit is 1 for the default executor not the TreadPoolExecutor which Mohamed is using. The default for the thread pool is much more than 1 (based on your link, it's somewhere around 5 with max of 128 as and when needed). – kha Aug 24 '15 at 08:30
  • It's really up to you to decide if it's a good idea to use all these async tasks. If they're sufficiently small it should be fine to do them this way. Maybe you should reconsider using an AsyncTask for your long running process (parsing the XML) though. Do it in a service and on a background thread (using threading mechanisms other than async task) as it's not recommended to run long running tasks inside an AsyncTask based on the documentation. I still don't get why it would be a serial execution though. By using the ThreadPoolExecutor (as you're doing based on your code), it shouldn't be. – kha Aug 24 '15 at 08:33

0 Answers0