0

At application launch time I am making network call (resttemplate Get call) to fetch data.

Scenario is something like this,

1)Parent call which returns list of tokens(Id's)

2)Once I got list of tokens(Id's),I iterate through it make network call for each token(Id) to get Data.

What I have done so for:

1)I have used Intent service to make parent network call to get list tokens(ID's).

2)Once I got list token(Id's) I started my AsyncTask executeOnExecutor,Passing list of tokens(ID's) to asynctask through constructor and starting AsyncTask executor. Something like this,

MyAsyncExecutorTask executorTask = new MyAsyncExecutorTask(List<Integer> of tokens);
executorTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,(Void[]) null);

And In MyAsyncExecutorTask this is what I am doing.

 protected Void doInBackground(Void...params) {

    //Settimg max priority
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

    for (int i = 0; i < scheduleList.size(); i++) { 
       ScheduleImpl scheduleImpl = new ScheduleImpl();
        //Making network call to get data using token
        scheduleImpl.getData(scheduleList(i));
    }

 }

Its working as it should be.

My doubts or Questions are:

1)Am I using Async executeOnExecuter() in right way(I want to make parallel network calls).I don't see any huge performance improvement after switching from AsyncTask execute() to Async executeOnExecuter().

2)How to check how many number worker threads running.

Joe
  • 550
  • 2
  • 11
  • 21

2 Answers2

0

You can make it like this:

AsyncTaskGetTokens.java

public class AsyncTaskGetTokens extends AsyncTask<Void,Integer, List<Integer>> {

    private ProgressDialog pd;
    private Context context;
    private String dataURL;

    public AsyncTaskGetTokens (Context context, Integer token)
    {
        this.act = act;
        this.token = token;
    }

    protected void onPreExecute()
    {
        this.pd = ProgressDialog.show(context, "", "");
    }

    protected Integer doInBackground(Void... arg0) {
        List<Integer> tokenList = new List<Integer>();
        //get token list from URL here
        return tokenList;
    }

    protected void onPostExecute(List<Integer> tokenList)
    {
        this.pd.dismiss();
        AsyncTaskGetData.tokensDownloading = new List<AsyncTaskGetData>();
        foreach(Integer token : tokenList)
        {
            AsyncTaskGetData.tokensDownloading.add(new AsyncTaskGetData(context, token); // here you create a list with all the tokens that will be downloaded
        }
        foreach(AsyncTaskGetData asynctask : AsyncTaskGetData.tokensDownloading)
        {
            asynctask.execute(); // here you will start downloading the data from each token
        }
    }

}

AsyncTaskGetData.java

public class AsyncTaskGetData extends AsyncTask<Void,Integer, Data> {

    public static List<AsyncTaskGetData> tokensDownloading;

    private ProgressDialog pd;
    private Context context;
    private Integer token;

    public AsyncTaskGetData (Context context, Integer token)
    {
        this.context = context;
        this.token = token;
    }

    protected void onPreExecute()
    {
        this.pd = ProgressDialog.show(context, "", "");
    }

    protected Data doInBackground(Void... arg0) {
        Data data = getDataFromURL(token);
        return data;
    }

    protected void onPostExecute(Data data)
    {
        //show data here
        this.pd.dismiss();
    }

}
Alexandre
  • 565
  • 3
  • 9
0

1)Am I using Async executeOnExecuter() in right way(I want to make parallel network calls).I don't see any huge performance improvement after switching from AsyncTask execute() to Async executeOnExecuter().

From Android Documentation

execute:

Executes the task with the specified parameters. The task returns itself (this) so that the caller can keep a reference to it.

Note: this function schedules the task on a queue for a single background thread or pool of threads depending on the platform version. When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting HONEYCOMB, tasks are back to being executed on a single thread to avoid common application errors caused by parallel execution

executeOnExecuter

Executes the task with the specified parameters. The task returns itself (this) so that the caller can keep a reference to it.

This method is typically used with THREAD_POOL_EXECUTOR to allow multiple tasks to run in parallel on a pool of threads managed by AsyncTask, however you can also use your own Executor for custom behavior.

Warning: Allowing multiple tasks to run in parallel from a thread pool is generally not what one wants, because the order of their operation is not defined. For example, if these tasks are used to modify any state in common (such as writing a file due to a button click), there are no guarantees on the order of the modifications. Without careful work it is possible in rare cases for the newer version of the data to be over-written by an older one, leading to obscure data loss and stability issues. Such changes are best executed in serial; to guarantee such work is serialized regardless of platform version you can use this function with SERIAL_EXECUTOR.

The question isn't about the performance improvement necessarily. When you run as executeOnExecuter, there is no guarantee that the calls will return in the order you made them. An execute method will schedule the task in a queue for a single background thread.

2)How to check how many number worker threads running.

You have the ability to define your own work queue, so if the question is how many threads can you run, then refer to this post Android AsyncTask threads limits?

I'm not sure if there is an easy way to check the count of current running. If there is I'm sure someone will be comment on that. But you can keep references to each task and check its status.

Community
  • 1
  • 1
paul_hundal
  • 371
  • 2
  • 9