0

I'm using an ad network to display some ads in my app. The loadAd function from their library sometimes causes the emulator to freeze (maybe because of some firewall issues).

Anyway, I've since moved to using AsyncTasks in which I do the actual call of that loadAd method (so the UI doesn't freze anymore) which requests the banner or whatever from their servers.

Whenever I restart my Activity, the onDetroy method of the Activity is called. In there, I call the AscynTask's cancel method so it will cancel the task and ... presumably stop/remove the thread.

Looking through the DDMS I'v noticed the below things:

See snapshot: http://bayimg.com/EAkBaAaEH

Every time I call the execute method of the AsyncTask, two AsynkTasks (#1 and #2) are created which seem to not go away (be killed) when I call the cancel(true) method in the `onDestroy().

After the Activity restarts, another two AsyncTasks threads are created (#3 and #4).

After another restart of the Activity one more AsynkTask is created (#5).

How can i actually kill the tasks? As in stop their execution entirely? Or do they actually stop and I'm just misinterpreting the DDMS?

AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106
  • 1
    When cancel is called asyntask doinBackground will continue its job and when it finishes, instead of onPostExecute .. OnCancelListener will be called – baboo Mar 09 '13 at 09:33
  • Well, after the Ad load and everything (after the things listed in the `doInBackground` are done), it still appears in the `DDMS`. Why is that? Why doesn't it disappear from DDMS? – AndreiBogdan Mar 09 '13 at 09:36

1 Answers1

1

Make sure you are not doing a while(true)(long continous task) kind of thing in Asynctask doinBackground, asynctask shld not be used for long/continous processes in background thread , if its a small background task it will finish and instead of onPostExecute oncancellistener will be called if cancelled was called during doinbackground execution...

as for still remaining in the DDMS check this : AsyncTask thread still there after execute, is that normal?

its nothing alarming just idle threads , As asynctasks run in a thread pool. idle threads wait for next asynctask to be executed

Community
  • 1
  • 1
baboo
  • 1,983
  • 17
  • 23
  • One more quick thing ... When the `onCancelled` method of an `AsyncTask` is called, will that force stop the execution of anything there is in the `doInBackground` method? If there is a `while(1)` in there, when I call `cancel(true)` will that "kill" everything, whatever it may be? Is it a guarantee that the thread will stop? Or would I have to do something in the `onCancelled` method to force the code to stop (depending on the code in the `doInBackground`. Maybe set a flag or something.)? – AndreiBogdan Mar 09 '13 at 09:43
  • 1
    there is a method isCancelled() in Asyntask check its boolean value in While(true) loop if true break out.. ... http://developer.android.com/reference/android/os/AsyncTask.html#isCancelled() – baboo Mar 09 '13 at 09:46
  • Yes, I was just looking at that 1 minute ago. :) Excellent. Thank you very much for your time. – AndreiBogdan Mar 09 '13 at 09:48
  • 1
    Also note asynctask is thread pool based , so if your threadpool limit is reached which i think is 5 (not exactly sure) new asynctask will go in wait state and will only execute when one previous thread finishes... just a heads up i had one asynctask with while(true) in service and few in activities and all of a sudden new asynctask stopped executing doinbackgrounds. that baffled me alot :) so lettin u know – baboo Mar 09 '13 at 09:51