0

I have written a foreground service which in the onCreate method does this:

public void onCreate(){
     //........
     startForeground(id,notification);
     Task1=new Task(this);
     task1.execute();
     Task2=new Task(this);
     Log.d("T2","Created");
     task2.execute();
     Log.d("T2","Executed");
}

Now what is happening is that above code is causing task1 to execute(which has a while(true) with sleep of 60 sec after each loop), but never lets task2 start its doInBackground().

I am not sure whats wrong.As per the logcat, its clearly showing that the task2.execute() does get invoked.But why is the doInBackground() of second task is not getting started? I was initially planning to have two foreground services, but after reading a few SO posts, decided to have only one Service in foreground and let it have two different Asynctasks to do some continuous processing in background. Please help me in making this work!

rahulserver
  • 10,411
  • 24
  • 90
  • 164
  • You could execute task2 from the onPostExecute of task1. Reason for doing that, and for your possible problem, is talked about here (look at the link in the accepted answer as well) http://stackoverflow.com/questions/11622675/multiple-asynctasks-from-one-activity – zgc7009 May 20 '14 at 19:33
  • Aside from the answers provided and the link provided by zgc7009, there is little advantage to using `AsyncTask` in a `Service` anyway. The `AsyncTask` is basically a hybrid with a worker thread and methods which allow access to the main thread to interact with UI elements. As a `Service` has no UI, most of the `AsyncTask` is redundant. In short, create and manage your own Threads. – Squonk May 20 '14 at 19:44
  • @Squonk are you sure about that? Services run in the UI thread (at least parts of them like `onStartCommand()`). Doesn't that necessitate the use of an AsyncTask in some cases, e.g. for networking? – matiash May 20 '14 at 19:49
  • read the documentation for `execute`. – njzk2 May 20 '14 at 19:51
  • @matiash : My point is the ability of `onPreExecute`, `onProgressUpdate` and `onPostExecute` to interact with the UI is redundant as a `Service` has no UI. I'm not saying the OP shouldn't use threads - I'm just saying using `AsyncTask` is not the right way to do it in a `Service`. – Squonk May 20 '14 at 19:53

2 Answers2

4

The default executor for AsyncTask, starting from Honeycomb, is AsyncTask.SERIAL_EXECUTOR, which means that only one task is executing at a given time.

If you need parallelism, just use AsyncTask.THREAD_POOL_EXECUTOR, i.e.

task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);

instead of just

task.execute(params);

See the Order of Execution section in the documentation for AsyncTask.

matiash
  • 54,791
  • 16
  • 125
  • 154
1

It`s simply because since from Android 3.0 AsyncTask-s work in a single background thread:

Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

http://developer.android.com/reference/android/os/AsyncTask.html

So Task2 never run because you run it before a completion of Task1.

DenisMath
  • 547
  • 4
  • 8