Android如何实现AsyncTask的并行执行 [英] Android how to implement parallel execution for AsyncTask
本文介绍了Android如何实现AsyncTask的并行执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要允许多个 AsyncTask 同时运行.这是我的 AsyncTask :
I need to allow multiple AsyncTask to be running at the same time . This my my AsyncTask :
private void callAPI(String user_id) {
new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... parameters) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_id", user_id));
return api.post("", params);
}//end doInBackground
protected void onPostExecute(String result) {
Log.i(TAG + "POST() => " + result);
}//end onPostExecute
}.execute(); //end AsyncTask
}
我在 running-multiple-asynctasks 但我不知道如何使用它.如何在我的 AsyncTask 中实现以下代码:
I saw an answer at running-multiple-asynctasks but I don't know how to use it . How can I implement the code below within my AsyncTask :
@TargetApi(Build.VERSION_CODES.HONEYCOMB) // API 11
void startMyTask(AsyncTask asyncTask) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
else
asyncTask.execute(params);
}
推荐答案
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Build;
/**
* An executor for concurrently executing AsyncTask tasks
* It takes all work for understanding device`s Android version
* and executes your AsyncTasks tasks concurrently
* @author Artem Zinnatullin (artem.zinnatullin@gmail.com)
* @version 1.2
*/
public class AsyncTaskExecutor {
private static final int CORE_POOL_SIZE;
private static final int MAXIMUM_POOL_SIZE;
private static final int KEEP_ALIVE;
private static final TimeUnit TIME_UNIT;
private static final BlockingQueue<Runnable> concurrentPoolWorkQueue;
private static final ThreadFactory concurrentThreadFactory;
private static final ThreadPoolExecutor concurrentExecutor;
private AsyncTaskExecutor() {}
static {
CORE_POOL_SIZE = 5;
MAXIMUM_POOL_SIZE = 128;
KEEP_ALIVE = 1;
TIME_UNIT = TimeUnit.SECONDS;
concurrentPoolWorkQueue = new LinkedBlockingQueue<Runnable>(10);
concurrentThreadFactory = new AsyncTaskThreadFactory();
concurrentExecutor = new ThreadPoolExecutor(
CORE_POOL_SIZE,
MAXIMUM_POOL_SIZE,
KEEP_ALIVE,
TIME_UNIT,
concurrentPoolWorkQueue,
concurrentThreadFactory
);
}
/**
* Concurrently executes AsyncTask on any Android version
* @param task to execute
* @param params for task
* @return executing AsyncTask
*/
@SuppressLint("NewApi")
public static <Params, Progress, Result> AsyncTask<Params, Progress, Result>
executeConcurrently(AsyncTask<Params, Progress, Result> task, Params... params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(concurrentExecutor, params);
} else {
task.execute(params);
}
return task;
}
/**
* Thread factory for AsyncTaskExecutor
* @author Artem Zinnatullin
*/
private static class AsyncTaskThreadFactory implements ThreadFactory {
private final AtomicInteger count = new AtomicInteger(1);;
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "AsyncTask #" + count.getAndIncrement());
}
}
}
如果要并发执行 Asynctask,请使用此类.找到了这个课程,它对我有用.
Use this class if you want to execute concurrently Asynctask. Found this class and its working for me.
AsyncTaskExecutor.executeConcurrently(new YourAsyncTask(),1);
异步任务
private class YourAsyncTask extends AsyncTask<Integer, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(mContext, "Please wait..Fetching Data",
Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Integer... params) {
Log.e("passing value",""+params[0]);
String uri = "http://abcd.com/ServiceTest.php?id="
+ params[0];
Log.e("passing value",""+uri);
return SetServerString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(SetServerString);
}
希望对你有帮助!
这篇关于Android如何实现AsyncTask的并行执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文