Should or should not use AsyncTask for long operations?

I am confused by what I read about AsyncTask. On the one hand, the Android document says that AsyncTasks should ideally be used for short operations (just a few seconds). . The document does not explain why this is so.

On the other hand, I have read at least one article that explicitly or implicitly suggests that AsyncTasks will be used for long-running operations, such as one .

Can any android hero give a more convincing reason why or why AsyncTask should not be used for lengthy operations? Or point me to documents that explain this. I could not find the answer.


And, someone asked a similar question . Let me see if there are answers for me.

+4
source share
2 answers

an interface opened with AsyncTask defines a setting, background action, and an "on done" callback that can update the user interface. this implies that it is intended for operations that have a clearly defined degree, which leads to user feedback.

if you need the current background thread, use Executor to execute the Runnable instance that hosts your current operations. use this with caution, as when your application leaves the foreground, the thread will continue to work. carefully create your Runnable so that it can be interrupted (the run() method can be called to return cleanly).

It is worth noting that AsyncTask simply forces you to clearly define a template for performing an asynchronous operation, and then update the interface. IMHO this is too complicated and makes a lot of assumptions, like a pool of threads with a single or limited size. for example, if one part of your application is blocked in AsyncTask s, do you assume that this will prevent AsyncTask from starting in another part of your application? it will be.

+2
source

Definitely not. Asynchronization tasks are queued, and startup is not possible until the previous one is complete.

Use regular threads for long tasks.

+1
source

All Articles