问题描述
AsyncTask 是同时工作还是先到先得? 。
例如,我有3个AsyncTask,它们具有相同的接口类和相同的侦听器功能。同时执行3个AsyncTask。在 listener 函数中将显示AsyncTasks的哪个响应?
疑问:
1。 AsyncTasks是并行运行还是先到先服务?
2.如果AsyncTasks以并行方式运行,那么如何为所有AsyncTasks处理相同的侦听器
函数?
Nb :怀疑2是因为在不使用AsyncTask的情况下同时执行多个请求时第一个响应已接收。 ( Web Api响应)。
预先感谢。
对于多个请求,您可以使用
如果(Build.VERSION.SDK_INT> = Build.VERSION_CODES.HONEYCOMB){
新callApi( ).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,location);
} else {
new callApi()。execute(location);
}
线程池模式
AsyncTask使用线程池模式来运行doInBackground()中的内容。
线程池模式是创建多个线程以执行多个任务的位置。基本上是一个容器,其中多个线程排在队列中以执行不同的任务。
例如:
公共类MultipleAsyncTask扩展了活动
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState) ;
runMultipleAsyncTask(); //启动异步任务
}
private void runMultipleAsyncTask()//运行多个异步任务
{
FirstAsyncTask asyncTask = new FirstAsyncTask(); //第一个
if(AppUtil.isCurrentVersionHoneycombAndAbove())//高于api级别13
{
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else //低于Api级别13
{
asyncTask.execute();
}
SecondAsyncTask asyncTask2 = new SecondAsyncTask(); //第二个
if(AppUtil.isCurrentVersionHoneycombAndAbove())//高于api级别13
{
asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else //低于api级别13
{
asyncTask2.execute();
}
}
//启动第一个异步任务:
私有类FirstAsyncTask扩展了AsyncTask< Void,Void,Void>
{
@Override
protected void onPreExecute()
{
Log.i( AsyncTask, FirstOnPreExecute());
}
@Override
受保护的void doInBackground(Void ... params)
{
for(int index = 0; index< 50; index ++)
{
Log.i( AsyncTask, FirstAsyncTask);
try
{
Thread.sleep(100);
}
catch(InterruptedException异常)
{
exception.printStackTrace();
}
}
返回null;
}
@Override
protected void onPostExecute(无效结果)
{
Log.d( AsyncTask, FirstonPostExecute());
}
}
//启动第二个异步任务:
私有类SecondAsyncTask扩展了AsyncTask< Void,Void,Void>
{
@Override
protected void onPreExecute()
{
Log.i( AsyncTask, SecondOnPreExecute());
}
@Override
受保护的void doInBackground(Void ... params)
{
for(int index = 0; index< 50; index ++)
{
Log.d( AsyncTask, SecondAsyncTask);
try
{
Thread.sleep(100);
}
catch(InterruptedException异常)
{
exception.printStackTrace();
}
}
返回null;
}
@Override
protected void onPostExecute(无效结果)
{
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请求)的相同asyncTask:
boolean标志;
@Override
受保护的字符串doInBackground(String ... params){
flag = params [0] .endsWith( / repos);
//其他语句
}
现在在onPostExecute中:
if(flag){
//解析一种方式
} else {
//解析另一种方式
}
Does AsyncTask works simultaneously or first come first served manner? .
For example i've 3 AsyncTasks that have same interface class and same listener functions. Executing 3 AsyncTasks at same time. Which response from AsyncTasks will shows in listener function?
Doubts:
1. Does AsyncTasks run parallel or first come first served manner? 2. If AsyncTasks run parallel manner how to handle same listener function for all AsyncTasks?
Nb: Doubt 2 is because first response recieving while doing multiple requests at same time without using AsyncTask. (Web Api response).
Thanks in advance.
For multiple request you can use ThreadPoolExecutor
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
new callApi().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, location);
} else {
new callApi().execute(location);
}
Thread Pool Pattern
AsyncTask uses a thread pool pattern for running the stuff from doInBackground()
The Thread pool Pattern is where number of Threads are created to perform a number of Tasks. It is basically a container where multiple threads come in a queue for different task.
For Example:
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()");
}
}
}
Output:
FirstOnPreExecute()
SecondOnPreExecute()
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstAsyncTask
SecondAsyncTask
FirstonPostExecute()
SecondOnPostExecute()
Same asyncTask for different functions like api requests:
boolean flag;
@Override
protected String doInBackground (String... params) {
flag= params[0].endsWith("/repos");
//other statements
}
Now in your onPostExecute:
if(flag){
//parse one way
} else {
//parse another way
}
这篇关于AsyncTask是否可以同时工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!