平行运行的AsyncTaskAsyncTask

2023-09-12 03:16:05 作者:可爱到不行

我在我的应用程序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.

推荐答案

您将需要使用线程池的Executor 执行的AsyncTask 。默认实现使用单个线程上运行的串行执行者

You will need to use a thread pool Executor to execute Asynctask . Default implementation uses a serial executor running on a single thread

因此​​,创建一个ThreadPoolExeecutor然后用

So create a ThreadPoolExeecutor and then use

AsyncTask的的executeonExecutor而不是仅仅执行方法

Asynctask's executeonExecutor instead of just execute method

 
精彩推荐
图片推荐