AsyncTask是同时工作还是先来先服务?
例如,我有3个具有相同接口类和相同监听器函数的AsyncTasks。同时执行3个AsyncTasks。哪个AsyncTask的响应将显示在监听器函数中?
疑问:
1. AsyncTask是并行运行还是先来先服务? 2. 如果AsyncTask以并行方式运行,如何处理所有AsyncTasks的相同监听器函数?
Nb:疑问2是因为在不使用AsyncTask进行多个请求时收到第一个响应。(Web Api响应)。
提前感谢。
AsyncTask是同时工作还是先来先服务?
例如,我有3个具有相同接口类和相同监听器函数的AsyncTasks。同时执行3个AsyncTasks。哪个AsyncTask的响应将显示在监听器函数中?
疑问:
1. AsyncTask是并行运行还是先来先服务? 2. 如果AsyncTask以并行方式运行,如何处理所有AsyncTasks的相同监听器函数?
Nb:疑问2是因为在不使用AsyncTask进行多个请求时收到第一个响应。(Web Api响应)。
提前感谢。
如果需要处理多个请求,可以使用ThreadPoolExecutor。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new callApi().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, location);
} else {
new callApi().execute(location);
}
线程池模式
AsyncTask使用线程池模式来运行doInBackground()中的任务。
线程池模式是指创建多个线程来执行多个任务的一种方式。它基本上是一个容器,多个线程在队列中等待不同的任务。
例如:
public class MultipleAsyncTask extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
runMultipleAsyncTask(); // Start Async Task
}
private void runMultipleAsyncTask() // Run Multiple Async Task
{
FirstAsyncTask asyncTask = new FirstAsyncTask(); // First
if(AppUtil.isCurrentVersionHoneycombAndAbove()) // Above Api Level 13
{
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else // Below Api Level 13
{
asyncTask.execute();
}
SecondAsyncTask asyncTask2 = new SecondAsyncTask(); // Second
if(AppUtil.isCurrentVersionHoneycombAndAbove())// Above Api Level 13
{
asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else // Below Api Level 13
{
asyncTask2.execute();
}
}
//Start First Async Task:
private class FirstAsyncTask extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
Log.i("AsyncTask" ,"FirstOnPreExecute()");
}
@Override
protected Void doInBackground(Void... params)
{
for(int index = 0; index < 50; index++)
{
Log.i("AsyncTask" ,"FirstAsyncTask");
try
{
Thread.sleep(100);
}
catch (InterruptedException exception)
{
exception.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
Log.d("AsyncTask" ,"FirstonPostExecute()");
}
}
//Start Second Async Task:
private class SecondAsyncTask extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
Log.i("AsyncTask" ,"SecondOnPreExecute()");
}
@Override
protected Void doInBackground(Void... params)
{
for(int index = 0; index < 50; index++)
{
Log.d("AsyncTask" ,"SecondAsyncTask");
try
{
Thread.sleep(100);
}
catch (InterruptedException exception)
{
exception.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
Log.d("AsyncTask" ,"SecondOnPostExecute()");
}
}
}
输出:
FirstOnPreExecute()
SecondOnPreExecute()
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstonPostExecute()
SecondOnPostExecute()
像api请求这样的不同功能可以使用相同的异步任务:
boolean flag;
@Override
protected String doInBackground (String... params) {
flag= params[0].endsWith("/repos");
//other statements
}
现在,在您的onPostExecute中:
if(flag){
//parse one way
} else {
//parse another way
}