idish
idish

Reputation: 3260

multiple http requests in my android app. Threading

I'm going to have around 10 or even more http post requests in my android app most of them are very short-time.

As I understand, I cannot perform any network operations on my UI thread.

The solution for this would be using the AsyncTask or using Thread.

If I'll be using the AsyncTask I will have to create to a customized class for that request.

If I'll be using Thread I will not have to implement a new class for the network operation.

So, my question is, when should I use the Thread and when should I use the AsyncTask option? Is it the right approach to create a thread for the short-time requests, and create a class that extends the AsyncTask on the longer-lived network operations?

Upvotes: 1

Views: 2926

Answers (2)

Gray
Gray

Reputation: 116828

when should I use the Thread and when should I use the AsyncTask option?

According to the documentation for AsyncTask:

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.

So it sounds like if you have a long running task you should fork your own Thread or use an ExecutorService. If you are just doing a short background task, AsyncTask is the way to go.

Is it the right approach to create a thread for the short-time requests, and create a class that extends the AsyncTask on the longer-lived network operations?

No, according to the above docs, you've got it backwards.

If I'll be using Thread I will not have to implement a new class for the network operation.

Both the AsyncTask and the Thread need you to declare a new class:

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {
       ...
    }
 }
 ...
 // to start it
 new DownloadFilesTask().execute(url1, url2, url3);

Here's how to use a thread:

 private class DownloadFilesTask implements Runnable {
      // if you need to pass in args via the constructor
      public DownloadFilesTask(URL... urls) {
          ...
      }
      public void run() {
          // do stuff in the background
      }
 }
 ...
 // to start it
 new Thread(new DownloadFilesTask(url1, url2, url3)).start();

If you need to get results from a background task, then you can also use a Callable and the ExecutorService classes instead:

How to make Callable wait till execution?

AsyncTask supports get() as well.

Upvotes: 1

matt b
matt b

Reputation: 139921

Even if you use Thread.start(), you still need to create a subclass of Runnable (or a subclass of Thread) to do the work in. You are creating new classes either way - maybe the only difference is if you are creating anonymous classes versus a top-level named class.

You shouldn't base your decision on whether or not you need to make a new class, since the cost is tiny - you create a new file, it has a different structure, etc. It's a simple one-time cost.

It seems to me like the decision should be based on whether or not you want all the extra logic and functionality that AsyncTask offers.

Upvotes: 1

Related Questions