Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
91 views
in Technique[技术] by (71.8m points)

android - running parallel AsyncTask

I have a SplashActivity in my application which do some stuff using AsyncTask when I start my application.

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.

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.

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There has been a change in AsyncTask from Honeycomb release. Older versions had a Thread pool of 10 threads, so you could run 10 tasks in parallel. But for Honeycomb and up, default is a serial executor, which executes tasks one by one. But you can pass a ThreadPoolExecutor for execution:

   if (Build.VERSION.SDK_INT >= 11) {
     //--post GB use serial executor by default --
     task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
   } else {
     //--GB uses ThreadPoolExecutor by default--
     task.execute();
   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...