AsyncTasks并行执行

时间:2015-05-10 11:49:04

标签: android android-asynctask

我对Async的执行有点怀疑。任务我想最小化我的应用程序执行的http请求所花费的时间,所以在互联网上的一些R& D之后我选择了下面描述的选项,我的问题是使用下面描述的代码,我的请求是否会被处理并行,如果是,这是最有效的方法,如果不是如何让它们并行执行,以便用户不必等待太多! 最诚挚的问候。

 if (isInternetAvailable) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {


                new DownloadXmlTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, URLORDERLIST);
                new DownloadXmlTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, URLORDERDETAILS);
                new DownloadXmlTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, URLCUSTOMERDETAILS);

            } else {


                new DownloadXmlTask().execute(URLORDERLIST);
                new DownloadXmlTask().execute(URLORDERDETAILS);
                new DownloadXmlTask().execute(URLCUSTOMERDETAILS);

            }


            handler.post(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(
                            getApplicationContext(),
                            "Fetch Data",
                            Toast.LENGTH_LONG).show();
                }
            });
        } else {
            handler.post(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(

                            getApplicationContext(),
                            "Please Try Again Later,No internet Connectivity Detected!",
                            Toast.LENGTH_SHORT).show();

                }
            });
        }

1 个答案:

答案 0 :(得分:3)

修改

  1. task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params)用于并行执行
  2. task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, params)用于连续执行
  3. task.execute()默认/常用选项