0

In my Android program, I have to do 4 or 5 network calls. Each are independent, and each can run on their own.

public void backGroundTasks() {

    new AsyncTasks<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {

                List<User> users = RetrofitAdapter.getClient().getUsersSync();

                saveToDB(users);

            } catch (RetrofitError error) {
                error.printStackTrace();
            }
            return null;
        }
    }.execute();

    new AsyncTasks<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {

                List<Media> contents = RetrofitAdapter.getClient().getContentsSync();

                saveToDB(contents);

            } catch (RetrofitError error) {
                error.printStackTrace();
            }
            return null;
        }
    }.execute();

    new AsyncTasks<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {

                Profile profile = RetrofitAdapter.getClient().getProfile();

                saveToDB(profile);

            } catch (RetrofitError error) {
                error.printStackTrace();
            }
            return null;
        }
    }.execute();

    new AsyncTasks<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            try {

                List<Message> messages = RetrofitAdapter.getClient().getMessages();

                saveToDB(messages);

            } catch (RetrofitError error) {
                error.printStackTrace();
            }
            return null;
        }
    }.execute();

}

My questions are,

How this AsyncTask work ? Will be executed in parallel ? or Will be invoked but the network requests will be queued ?

In some SO answers they have mentioned to do execute AsyncTasks with executeOnExecutor(). Will it help for this scenario ?

Note: I use Retrofit library for making REST calls easier. Does it have anything with this problem ?

Kamalakannan J
  • 2,818
  • 3
  • 23
  • 51

3 Answers3

1

How this AsyncTask work ? Will be executed in parallel ?

Since honeycomb they will be executed serially, but if you will use executeOnExecutor then they will be executed in parallel.

or Will be invoked but the network requests will be queued ?

they will be queued, no network operation will start untill AsyncTask will get its turn.

Internally AsyncTasks use Executors. Your tasks only execute network operation, so you might switch to Executors. If you want to do your network operations serially, then you might also consider using IntentService.

marcinj
  • 48,511
  • 9
  • 79
  • 100
1

This is answer by Arhimed. He explained it well.

AsyncTask uses a thread pool pattern for running the stuff from doInBackground(). The issue is initially (in early Android OS versions) the pool size was just 1, meaning no parallel computations for a bunch of AsyncTasks. But later they fixed that and now the size is 5, so at most 5 AsyncTasks can run simultaneously. Unfortunately I don't remember in what version exactly they changed that.

For more check his answer

Community
  • 1
  • 1
Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
1

Executors with AsyncTasks works like charm. It executes the network requests concurrently.

From the docs,

Since Honeycomb, invoking AsyncTask's executeOnExecutor() instead of execute() will execute tasks in parallel.

AsyncTask.THREAD_POOL_EXECUTOR

An Executor that can be used to execute tasks in parallel.

Kamalakannan J
  • 2,818
  • 3
  • 23
  • 51