Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asynctask doInBackgound() not running if there's a asynctask already running

When the user logs in into my app. I am starting an asynctask to maintain the user session. And that async task is running indefinitely till the user logs out. My problem is that when I try to start other asynctasks, their doInBackground() method is never executed.

I read somewhere that if an async task is already running, we cannot start new async task. I can confirm this because when i removed the user session async task, it worked properly. Is there a workaround?

P.S.: I have already used executeOnExecutor() method. but it didn't help.

like image 233
Rookie Avatar asked Dec 11 '13 13:12

Rookie


Video Answer


2 Answers

For potentially long running operations I suggest you to use Service rather than asynctask.

Start the service when the user logs in

Intent i= new Intent(context, YourService.class);
i.putExtra("KEY1", "Value to be used by the service");
context.startService(i); 

And stop the service when the user logs out

stopService(new Intent(this,YourService.class));

To get to know more about Service you can refer this

Service : Android developers

Service : Vogella

To know more about asynctask vs service you can refer this

Android: AsyncTask vs Service

When to use a Service or AsyncTask or Handler?

like image 193
Abhishek V Avatar answered Sep 20 '22 21:09

Abhishek V


I read somewhere that if an async task is already running, we cannot start new async task.

Yes,That is fact that you can't run more then 5 (five) AsyncTaskat same time below the API 11 but for more yes you can using executeOnExecutor.

AsyncTask uses a thread pool pattern for running the stuff from doInBackground(). The issue is initially (in early Android OS versions) the pool size was just 1, meaning no parallel computations for a bunch of AsyncTasks. But later they fixed that and now the size is 5, so at most 5 AsyncTasks can run simultaneously.

I have figure out Some Threading rules and i found one major rule is below ,

The task can be executed only once (an exception will be thrown if a second execution is attempted.) 

What is definition of AsyncTask?

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

How & Where use it?

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended to use it.

Why you can't use multiple AsyncTask at same time ?

There are a few threading rules that must be followed for this class to work properly:

  • The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.

  • The task instance must be created on the UI thread.

  • execute(Params...) must be invoked on the UI thread.

  • Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.

  • The task can be executed only once (an exception will be thrown if a second execution is attempted.)

  • Running multiple AsyncTasks at the same time — not possible?

  • Test sample of parallel excution of AsyncTasks

Try Executor

You should go with Executor that will mange your multiple thread parallel.

Executor executor = anExecutor;
executor.execute(new RunnableTask1());
executor.execute(new RunnableTask2());
...
  • Sample Example 1
  • Sample Example 2
like image 33
Chintan Khetiya Avatar answered Sep 22 '22 21:09

Chintan Khetiya