[英]How to run multiple background tasks in parallel using AsyncTask in android..?
I am a beginner in android development. 我是android开发的初学者。 I ve already tried using simple execute() to do 1 task and it worked well.
我已经尝试使用简单的execute()来完成1个任务,并且效果很好。 But i have now come across a problem where i need to perform 3-4 tasks concurrently in background using AsyncTask.
但是我现在遇到一个问题,我需要使用AsyncTask在后台同时执行3-4个任务。 I have seen several questions on this but i dint get to see a code for doing the same.
我已经看到了一些关于此的问题,但我确实看到了执行此操作的代码。 I think executeOnExecutor() is the answer to my question but i want to see an example of it.
我认为executeOnExecutor()是我的问题的答案,但我想看一个例子。 Can someone please explain me with a simple example of how to run say 2 tasks concurrently using AsyncTask ?
有人可以用一个简单的示例向我解释一下如何使用AsyncTask同时运行两个任务吗?
The docs for AsyncTask cover this. AsyncTask的文档对此进行了介绍。 By default, all of the AsyncTasks happen on a single thread.
默认情况下,所有AsyncTasks都在单个线程上发生。 To use multiple threads, you need to use a different executor.
要使用多个线程,您需要使用其他执行程序。 AsyncTask has a thread pool executor you can use:
AsyncTask有一个线程池执行程序,您可以使用:
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
I would strongly recommend not using multiple Async tasks. 我强烈建议不要使用多个异步任务。 I did that before and ran into a ton of issues.
我之前做过,然后遇到了很多问题。 See this SO post .
看到这个帖子 。 What happens is one Async Task will wait for the other to finish before starting.
发生的是一个异步任务将在启动之前等待另一个异步任务完成。 Try using a runnable:
尝试使用可运行的:
Runnable myRunnable = new Runnable() {
@Override
public void run() {
//Code
}
};
new Thread(myRunnable).start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.