Take the 2-minute tour ×

AsyncTask's are mostly presented as a solution to running well-defined computations or tasks. The classic example is a downloader as an AsyncTask which is fed a URL, updates its progress to the UI, and exits when completed. Is there a reasonable (not-too-hacky) way to add/change input to the AsyncTask while it's running? Perhaps setting up a queue of items that the AsyncTask has to process and then altering that queue from the main application...? Would that 'break' the AsyncTask model for example?

share|improve this question
a commonly accepted way would be to create a new asynctask for each new item to process. using a custom executor, your tasks can be serialized, too, making it look quite like a queue –  njzk2 Nov 29 '12 at 10:57
You might want to check the IntentService class if you want to perform one task after another –  Matt Quiros Nov 29 '12 at 11:05
+1 to @Matt Quiros. Spawning multiple long-running async tasks is not recommended. Moreover, the behavior may be different on different OS versions. Services or just threads/handlers and the like seem to be a better choice in your case. –  full.stack.ex Nov 29 '12 at 11:16
what is your task doing so that you want to update its input ? –  NikkyD Nov 29 '12 at 11:28
add comment

2 Answers

up vote 2 down vote accepted

Yes you can. AsyncTask is a thread that has already buited in communication mechanisms to the UI thread. However this communication is only from AsyncTask to UI and not in the reverse direction as you need.

Also, the maximum number of simultaneous AsyncTask in Android is 1 in several API versions.

To implement thread safe communication from UI to AsyncTask you will need to use Handler or BlockingQueue classes.

Personally, I wouldn't do it. I would use directly a Thread with a Handler or BlockingQueue.


share|improve this answer
add comment

Yes, you can simply do it by getting a object in the doInBackground() metheod from a synchronized collection that is managed by your activity.

share|improve this answer
add comment

Your Answer


