Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncTask v/s ThreadPoolExecutor for network request

I am working on a project where i need to hit a web service download the data which is JSON and will be represented in a list. And all the list items have there thumbnail url's which would be downloaded and displayed in the list item.

I have done the entire calling part with both a ThreadPoolExecutor and a AsyncTask.

But from a design perspective which is a better option out of:
1. ThreadPoolExecutor
2. AsyncTask

Few adv. of ThreadPoolExecutor:
1. User can define no. of concurrent threads that can be executed.
2. Cancelling all the request at once.

Few adv. of AsyncTask:
1. Internally it uses a ThreadPoolExecutor, however we cant define no. of threads that run simultaneously.
2. Cancelling a single request is easy.
3. Ability to attach and detach a task.
4. Updating the UI from doInBackground is simple.

I know more advantages of AsyncTask, however for a simple application like fetching data from a web service and later on fetching images.

Which would be more appropriate a AsyncTask or ThreadPoolExecutor? If you can provide a few reasons regarding your choice it would be helpful.

I have read a few article here on SO but none that compares the two. If there are any that i missed sorry for the trouble could you please post me the link for the same.

Thanks in advance.

like image 880
Jayshil Dave Avatar asked Oct 10 '11 18:10

Jayshil Dave


People also ask

What is the difference between handler vs AsyncTask vs thread?

Using Handlers you have the advantage of MessagingQueues , so if you want to schedule messages or update multiple UI elements or have repeating tasks. AsyncTask are similar, in fact, they make use of Handler , but doesn't run in the UI thread, so it's good for fetching data, for instance fetching web services.

Why is AsyncTask deprecated?

This class was deprecated in API level 30.AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.

What is the difference between AsyncTask and thread runnable?

This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework.

What can I use instead of AsyncTask in Android?

Alternative 1: Using Executor and Handler The executor will help in performing any task in the background and the handler will help to make UI changes.


1 Answers

I consider that AsyncTask is useful if you want to load thumbnail using a series "cascade-call": in the onPostExecute, you can start the next AsyncTask to download the next thumbnail.

But if you want to improve efficiency, I suggest using ThreadPoolExecutor. This is a sentence from developer.android.com:

Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.

In conclusion, ThreadPoolExecutor was probably designed for cases such as your; for this reason, I suggest you this class.

like image 115
Vito Gentile Avatar answered Sep 21 '22 13:09

Vito Gentile