tags:

views:

50

answers:

1

I have an Android Service that is started by my application and does some things in a threadPool using Executors.newCachedThreadPool()

Once it has finished doing it's work I would like the service to stopSelf(), how can I get the service to determine when it is no longer needed (ie, there are no more Threads executing) so that it can shut itself down automatically?

A: 

Do it the other way: the last Runnable should shut the Service upon completion.

alex
But I don't know what the last runnable will be.
jax
@jaxTo know which `Runnable` will be the last one, you can a)use a single-threaded `Executor` or b)implement a simple simple counter that will be incremented each time you submit a `Runnable` and then decremented by `Runnable` upon completion.
alex