AsyncTask threads never die

AndroidMultithreadingAndroid Asynctask

Android Problem Overview


I'm using AsyncTasks to fetch data in response to the user pressing a button. This works well and keeps the interface responsive while fetching the data, but when I checked out what was going on in the Eclipse debugger, I found out that every time a new AsyncTask was created (which is quite often, because they can only be used once), a new thread was being created but never terminated.

The result is a large number of AsyncTask threads just sitting there. I'm not sure if this is a problem in practice or not, but I'd really like to get rid of those extra threads.

How can I kill these threads?

Android Solutions


Solution 1 - 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.

Solution 2 - Android

In addition to CommonsWare's response:

Currently I'm using Android 2.2, and my application uses no more than one AsyncTask at any time, but I'm creating a new one every x minutes. At first new AsyncTask Threads start to appear (a new Thread for a new AsyncTask) but after 5 threads (as mentioned by CommonsWare) they just stay visible in the debug window, and get re-used when new AsyncTask threads are needed. They just stay there until the debugger disconnects.

Solution 3 - Android

Same symptom here. In my case the threads hanged around after I'd killed the Activity, and I was hoping for the App to close completely. Problem partly solved by using a single threaded executor:

    myActiveTask.executeOnExecutor(Executors.newSingleThreadExecutor());

This made the thread to vanish after the completing its work.

Solution 4 - Android

Use ThreadPoolExecutor.

BlockingQueue workQueue= new LinkedBlockingQueue<Runnable>(100); // Work pool size
ThreadPoolExecutor executor = new ThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors(),       // Initial pool size
            Runtime.getRuntime().availableProcessors(),       // Max pool size
            1, // KEEP_ALIVE_TIME
            TimeUnit.SECONDS, //  KEEP_ALIVE_TIME_UNIT
            workQueue);

Post your Runnable task by using execute() method.

void execute (Runnable command)

>Executes the given task sometime in the future. The task may execute in a new thread or in an existing pooled thread

Regarding your second query:

>How can I kill these threads?

Once you are done with all your work with ThreadPoolExecutor, shutdown it properly as quoted in below post:

https://stackoverflow.com/questions/36644043/how-to-properly-shutdown-java-executorservice/36644320#36644320

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionComputerishView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - AndroidTom de WaardView Answer on Stackoverflow
Solution 3 - AndroidkarnboView Answer on Stackoverflow
Solution 4 - AndroidRavindra babuView Answer on Stackoverflow