Asynctask x Multithreading

Asked

Viewed 257 times

6

When it is most advantageous to use Asynctask, and when to use Threads.

For example:
Download a file (e.g., JSON).

Which would be more advantageous in this case and why??

  • After all Thread or Service or AsyncTask? I haven’t been enlightened at all.

2 answers

6


If it is a simple¹ JSON file, use Asynctask.

Asynctask is most useful for asynchronous operations (d'oh) with low data demand; on the other hand, however, the Java Threads are relevant when dealing with high-demand data, GUI operations and hardware-intensive applications running on background.

As good references, you can read Asynctask and Threads for Android.

¹: small, trivial, without a lot of demand.

  • 1

    I don’t agree with that answer. AsyncTasks use Threads under the hood, and both should only be used for short-term operations (for longer operations, use Services). The advantage of AsyncTask is to simplify asynchronous operations that must access the UI thread (i.e., update the screen) before, during or after the operation itself, dispensing with the use of Handlers to make these updates (without these features, Threads won’t even access the GUI).

  • In your reply, you quoted Service and AsyncTask - at the end of the day, through the use of however I don’t know what your best guess is. Anyway, as a replica, I agree that Threads run underneath Asynctasks - the documentation itself says so - but the question is, in his case, better a "pure" thread or an Asynctask? I go from Asynctask - this is the focus of the answer to the question in question.

  • Finally, http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html, http://www.mergeconflict.net/2012/05/java-threads-vs-android-asynctask-which.html, http://stackoverflow.com/questions/18480206/asynctask-thread-in-android-android and http://stackoverflow.com/questions/12797550/android-asynctask-for-long-running-Operations. Therefore, based on everything you said, I no longer know what to think.

  • Looking at the links I believe you’re right, Threads are feasible for longer downloads. I found that Services would have higher priority and durability in the system, but I may be wrong. I will rephrase my answer. Only two things: the Vogella.com proposes so much Threads as Services (here) to download, so it does not help to clarify this point; and the excerpt "operations with the GUI" needs to be clarified, as I said threads other than the UI thread are not able to access the GUI.

6

AsyncTasks use Threads under the table, and serve to simplify the display on the screen of the progress of the operation being made in background (before, during or after its execution). But as AsyncTasks are loosely linked to the life cycle of Activity and can also cause memory Leaks if they are poorly implemented, one should give preference to their use for very short operations (at most a few seconds).

For longer operations you can use Threads or Services (in the first case display the result on the screen involves the use of a Handler, and in the second the use of a BroadcastReceiver).

To download files so use Thread or Service. However, to receive the return of a request to a webservice (the JSON case you mentioned) where the amount of data received is small and this data needs to be displayed on the screen, it is simpler to use AsyncTask.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.