how to run AsyncTask parallelly or independently in android - android

I would like to send more than one request to server parallelly using AsyncTask in android
so how can i do that ?
I have seen code like
myAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params)
but it is not running parallelly instead it is running serially.
please help me out.

Hey the executeonExecutor should work perfectly.
You will need to use a thread pool Executor to execute Asynctask . Default implementation uses a serial executor running on a single thread
So create a ThreadPoolExeecutor and then use
Asynctask's executeonExecutor instead of just execute method
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();
}

Create new instance of the async task and execute, then it will execute parallelly

simply... use
new YourAssynctask().execute();
this will indeed call your Assyntask's OnpreExecute() method.

Related

How to elevate the priority of AsyncTask?

Is there a way to elevate the priority of an AsyncTask?
I'm doing some image manipulation in an AsyncTask. On a very slow device, it takes up to 60 seconds to process an image. Now I'd like to increase the performance by elevating the priority of the task. Can it be done somehow?
Try to use following (increase of priority AsyncTask threads):
protected final MyResult doInBackground(MyInput... myInput) {
Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND + THREAD_PRIORITY_MORE_FAVORABLE);
// blah-blah
}
If you do not have a heavy UI which would interleave with the AsyncTask flow of execution, then your problem is in the algorithm used.
If you can divide your algorithm in parallel tasks, then you can use a pool of executors. If not, your Async Task is just doing serial work.
Be aware that according to AsyncTask:
When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.
If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR
You can use code like this on most devices
if (Build.VERSION.SDK_INT >= 11) {
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
asyncTask.execute();
}
or even create a custom ThreadPoolExecutor.
Set thread priority inside your AsyncTask doInBackground:
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Like following:
protected Object doInBackground(Integer... params) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
...
...
}

Set 'DiscardPolicy' on 'ThreadPool' created using 'ExecuteOnExecutor' method

I have a class called LoadEncryptedImage and that is derived from AsyncTask.
I used the below code to execute the async tasks in parallel,
LoadEncryptedImage loadEncryptedImage = new LoadEncryptedImage(mContext, eventMember.MemberPhotoURL,
viewHolder.imgUser);
loadEncryptedImage.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
It works properly, but it throws RejectedExecutionException if the tasks exceeds the limit of the ThreadPool. I want to set DiscardPolicy on ThreadPool to discard the issue silently. But how we can do this on a Threadpool created using ExecuteOnExecutor method.
But how we can do this on a Threadpool created using ExecuteOnExecutor method.
That is not a good idea. Do not change the characteristics of a thread pool that you did not create, as other things that depend upon that thread pool may not appreciate the changes that you make.
Instead, create your own ThreadPoolExecutor, with your desired characteristics, and pass that into executeOnExecutor().

When i start AsyncTask two time, second start when first finished [duplicate]

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.
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();
}
You will need to use a thread pool Executor to execute AsyncTask. Default implementation uses a serial executor running on a single thread
So create a ThreadPoolExecutor and then use
AsyncTask's executeOnExecutor instead of just execute method
Another option is using AsyncTaskCompat from support v.4 library.
AsyncTaskCompat.executeParallel(new AsyncTask<Void, Void, Object>() {
#Override
protected Object doInBackground(Void... params) {
// do your parallel task here
return null;
}
#Override
protected void onPostExecute(Object result) {
// and fetch result
}
});

How to limit the threads in the thread pool (Android)

When executing AysncTask, The following api I am using
executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"nameofpool");
Is it possible that somehow I can set only 2 threads limit in this pool.
The AsyncTask.THREAD_POOL_EXECUTOR is a special pool that is created for you and administrated by Android.
You can, however, create your own Executor, typically using :
Executor myExecutor = Executors.newFixedThreadPool(2);
which you can use in your AsyncTask :
executeOnExecutor(myExecutor, params);
Nota: please note that your param "nameofpool" is actually the parameter to the doInBackground method on your AsyncTask, and is not related to the Thread pool management.
You can provide your own executor:
executeOnExecutor(Executors.newFixedThreadPool(2), "nameofpool");

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.
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();
}
You will need to use a thread pool Executor to execute AsyncTask. Default implementation uses a serial executor running on a single thread
So create a ThreadPoolExecutor and then use
AsyncTask's executeOnExecutor instead of just execute method
Another option is using AsyncTaskCompat from support v.4 library.
AsyncTaskCompat.executeParallel(new AsyncTask<Void, Void, Object>() {
#Override
protected Object doInBackground(Void... params) {
// do your parallel task here
return null;
}
#Override
protected void onPostExecute(Object result) {
// and fetch result
}
});

Categories

Resources