问题描述
我的应用程序中有一个 SplashActivity,它在我启动应用程序时使用 AsyncTask 做一些事情.
I have a SplashActivity in my application which do some stuff using AsyncTask when I start my application.
我还创建了另一个从服务器下载数据的 AsyncTask.现在,在我关闭应用程序后,AsyncTask 仍在下载数据.
I have also created another AsyncTask which downloads data from the server. Now after I close my application the AsyncTask is still downloading the data.
但是当我再次启动我的应用程序时,我的 SplashActivity 的 AsyncTask 不会执行它的后台(doInBackground 函数),直到我的下载器 AsyncTask 完成,并且我的应用程序被挂起.
But when I again start my application my SplashActivity's AsyncTask does not execute its background (doInBackground function) till my downloader AsyncTask is finished, and my application is hanged.
所以我的问题是我们不能并行运行两个 AsyncTask?两个 AsyncTask 都是不同的,并且都在做自己的事情.有没有办法做到这一点.
So my question is it that we cant run two AsyncTask parallel? Both AsyncTask are different and doing their own stuff.Is there a way to do it.
推荐答案
您将需要使用线程池执行器
来执行AsyncTask
.默认实现使用在单线程上运行的串行执行器
You will need to use a thread pool Executor
to execute AsyncTask
. Default implementation uses a serial executor running on a single thread
所以创建一个 ThreadPoolExecutor 然后使用
So create a ThreadPoolExecutor and then use
AsyncTask 的 executeOnExecutor 而不是仅仅执行方法
AsyncTask's executeOnExecutor instead of just execute method
这篇关于运行并行 AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!