Asynctask Threads Never Die

AsyncTask threads never die

AsyncTask manages a thread pool, created with ThreadPoolExecutor. It will have from 5 to 128 threads. If there are more than 5 threads, those extra threads will stick around for at most 10 seconds before being removed. (note: these figures are for the presently-visible open source code and vary by Android release).

Leave the AsyncTask threads alone, please.

AsyncTask thread never ends

Found out what is going on here, and it wasn't what I thought. I'll detail it here as it may be useful to somebody. Has nothing to do with other AsyncTask threads and thread pooling.

In the IabHelper class are two functions flagStartAsync() and flagEndAsync(). The aim of these is to produce a single pass gate (bit like wait() and signal() in traditional multi-threading) so only one async operation (that is, communications with Google Play and the server) can occur at a time. If flagStartAsync() get called while something is already going on, it produces an exception. Not terribly graceful, but effective I guess.

The flagStartAsync() 'test and set' gets called at the start of launchPurchaseFlow() among other places, and flagEndAsync gets called in handleActivityResult() - again - among other places. So providing the purchase flow does something that always produces a result, no problem. The problem is - it doesn't always.

If you look at launchPurchaseFlow() there are several paths out that will not kick off the async operation, and if one of those get taken, mAsyncInProgress (the relevant flag) gets left set.

What blew it in my case was that I hadn't checked that the item was already purchased, and 'already purchased' is one of the paths out. Mea culpa, but the problem is that I cannot convince myself that there aren't several other paths that you just cannot avoid at times. What if operation is slow and the 'purchase' button gets pressed twice, for instance? I bet there are others as well. One could catch the exception, and that would stop a crash, but it wouldn't really help if nothing came along to clear the flag in the end. I guess the exception handler could call flagEndAsync() but it has an uncomfortable 'sledgehammer to crack a nut' feel.

It strikes me that this is probably a non-robust piece of code. What I've done for now is call flagEndAsync() in the various ways out of launchPurchaseFlow(), but that is just a temporary fix. I don't know enough about the IabHelper code, but I think it needs more careful thought and I need to analyse it to see everything it does.

Android: Properly destroying asyncTask?

AsyncTask uses a thread pool. It is normal for you to see 4/5 async tasks in your debug panel. Just make sure that your async tasks do not hold strong references to the activity (try to make those async tasks static inner classes (or event separate classes) and have them hold a WeakReference to the activity instead of a strong reference.

  • Background task, progress dialog, orientation change - is there any 100% working solution?
  • AsyncTask threads never die
  • Simple Thread Management - Java - Android
  • etc. > Try to search "android asynctask thread pool" to learn more.

Android AsyncTask remains in running state after completing

I was banging my head on this as well, and just came across this answer (well, it's more an explanation than an answer):

CommonsWare explains:

AsyncTask manages a thread pool, created with ThreadPoolExecutor. It will have from 5 to 128 threads. If there are more than 5 threads, those extra threads will stick around for at most 10 seconds before being removed. (note: these figures are for the presently-visible open source code and vary by Android release).

Leave the AsyncTask threads alone, please.

Source question:
AsyncTask threads never die

AsyncTask is still running after onPostExecute() in debug window

Just for completeness and so I can accept the question, here is the answer, given by grv_9098, found at stackoverflow.com/a/3077508/1514187 :

AsyncTask manages a thread pool, created with ThreadPoolExecutor. It will have from 5 to 128 threads. If there are more than 5 threads, those extra threads will stick around for at most 10 seconds before being removed. (note: these figures are for the presently-visible open source code and vary by Android release).

Leave the AsyncTask threads alone, please.

What kills an Android AsyncTask?

Ok guys I'm sorry but I have to tell you you are wrong.
An AsyncTask is (almost) NEVER killed. Calling AsyncTask.cancel will NOT kill the task.

Quoted From the documentation of AsyncTask

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

reading docs can be useful... So i'll sum it up for you, but since I already answered in another post, I send you to it : How can I make asynchronous URL connections on Android?

I gave a workaround to your problem, and yes, some time, some AsyncTask are killed. But I won't recommend using them, they're useless. But since, your AsyncTasks running should show a "waiting" status if they're done with the task. Otherwise your task is NOT done, and I never heard about some adb thread watching bug.

Regards !

How to limit the number of threads launched using ASyncTask?

Right now, unless you are willing to limit yourself to API Level 11 (Android 3.0) and higher, you cannot limit the number of threads used by AsyncTask. IIRC, it will use up to 20 threads maximum.

If you want fewer than that, you will need to create your own thread pool mechanism and use that instead of AsyncTask. Or, clone the code from AsyncTask into your own project and modify the characteristics of its own thread pool.

AsyncTask never executing doInBackground (but does in debug) (Update: Fixed)

If it seems to not be executing, something might be throwing an Exception in doInBackground, in which case the LogCat will have a stacktrace from the background thread (maybe something like siteRoot is null?)

Why does AsyncTasks not get destroyed in Android

AsyncTask manages a thread pool, created with ThreadPoolExecutor. It
will have from 5 to 128 threads. If there are more than 5 threads,
those extra threads will stick around for at most 10 seconds before
being removed. (note: these figures are for the presently-visible open
source code and vary by Android release).

Leave the AsyncTask threads alone, please

Source question: AsyncTask threads never die



Related Topics



Leave a reply



Submit