Android 并行执行多个 AsyncTask
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12159403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing Multiple AsyncTask's Parallely
提问by Srht
I'm using Android SDK 4.0.3 API15 and I want to run multiple AsyncTasks parallely. I'm getting my data from web server and animating(10 fps) it real-time. But depending on user operations I need to send some data to web server also. When this occurs my animation pauses for a short time (sending data gets into queue and getting data waits it to finish ) and therefore I can't catch the real-time.
我正在使用 Android SDK 4.0.3 API15 并且我想并行运行多个 AsyncTasks。我从网络服务器获取我的数据并实时动画(10 fps)。但是根据用户操作,我还需要向 Web 服务器发送一些数据。发生这种情况时,我的动画会暂停一小段时间(发送数据进入队列并获取数据等待它完成),因此我无法捕捉实时。
This answeris quite explaining but I couldn't make it work. So any help will be very appreciated.
这个答案很好解释,但我无法让它发挥作用。所以任何帮助将不胜感激。
I think I need to use this function to achieve that:
我想我需要使用这个功能来实现:
AsyncTask.executeOnExecutor(Executor exec, Params... params)
But I can't pass an executor as a parameter and I can't instantiate an executor. This is my AsyncTask class:
但是我无法将执行程序作为参数传递,也无法实例化执行程序。这是我的 AsyncTask 类:
public class GetPlayers extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
rawData="";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
if((rawData = buffer.readLine()) == null){
rawData = "error";
}
} catch (Exception e) {
e.printStackTrace();
}
}
return rawData;
}
@Override
protected void onPostExecute(String result) {
manipulate();
}
}
And I execute it like this:
我是这样执行的:
GetPlayers task = new GetPlayers();
requestString = "web adress is here...";
task.execute(new String[] { requestString });
回答by Marcin Orlowski
This is how I do that:
这就是我这样做的方式:
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
new MyAsyncTask().execute();
}
where MyAsyncTask
is regular AsyncTask subclass. Or you can wrap this all in helper class:
MyAsyncTask
常规 AsyncTask 子类在哪里。或者你可以把这一切都包装在 helper 类中:
class TaskHelper {
public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task) {
execute(task, (P[]) null);
}
@SuppressLint("NewApi")
public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task, P... params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
} else {
task.execute(params);
}
}
}
and then just do:
然后就做:
TaskHelper.execute( new MyTask() );
or
或者
TaskHelper.execute( new MyTask(), args );
or
或者
TaskHelper.execute( new MyTask(constructorParams), args );
回答by Jan-Robert Windgassen
This problem occured to me, when updating my app in AndroidStudio to sdk 25 from Intelliy using Android 2.3 Just the little change (executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, string1, ...); ) works perfectly. I have an AsyncTask running in a Service for download and create AsyncTasks for upload in no service.
这个问题发生在我身上,当我使用 Android 2.3 从 Intelliy 更新我在 AndroidStudio 中的应用程序到 sdk 25 只是一点点变化 (executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, string1, ...); ) 工作完美。我有一个 AsyncTask 在服务中运行以供下载,并创建 AsyncTasks 以在没有服务的情况下上传。