Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing Multiple AsyncTask's Parallely

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.

This answer is 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:

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 }); 
like image 649
Srht Avatar asked Aug 28 '12 12:08

Srht


People also ask

Can we run multiple async task at a time?

There is a limit of how many tasks can be run simultaneously. Since AsyncTask uses a thread pool executor with limited max number of worker threads (128) and the delayed tasks queue has fixed size 10, if you try to execute more than 138 your custom tasks the app will crash with java.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.

Do Async execute make threads in parallel or serial?

If you are running on Android 1.5, they will be executed serially. Otherwise, they will be executed in parallel.

Why did AsyncTask get deprecated?

This class was deprecated in API level 30. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.


1 Answers

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:

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 ); 
like image 100
Marcin Orlowski Avatar answered Sep 17 '22 02:09

Marcin Orlowski