question

singdev avatar image
singdev asked

PurchaseUpdatesAsyncTask thread does not finish

Hi, I am running your sample code on a test device and using Eclipse. However, when I call PurchaseUpdatesAsyncTask, the doInBackground and onPostExecute on that class do get called, but the thread keeps running on Eclipse. I even tried a GC from within Eclipse but the thread is still alive. Why does the thread not get recycled/killed? Thanks, Vipul
iap
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
AsyncTask uses "thread pool" technique. Each AsyncTask you start gets into a queue; there are some idle threads in the "pool" (or they are created as needed up to a certain limit) waiting for tasks. An idle thread from the pool takes your AsyncTask and executes it, then returns to the pool. Then the process repeats until there are no more tasks in the queue. This approach has 2 important features: no overhead for creating a thread every time in case of huge number of tasks system performance degrades gracefully: most of the tasks will wait in the queue and only few of them will be executed at a time; eventually all of them will get executed. Otherwise, if a separate thread was started for each task, the system would likely run out of memory or threads, or tasks will take forever to finish. The thread which you see in DDMS after your AsyncTask finished is the idle thread in the pool.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

singdev avatar image
singdev answered
Understood. Thanks!
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.