2

I want my executor to run up until my service is not running anymore.

The logical solution I think of is to do executor.shutdown() on the service's onDestroy()

What would be the consequences of that action?

I know when I kill an app it's services doesn't guarantee to go through onDestroy(), hence the executor will never preform a shutdown()

If the service is a foreground service, will it solve that issue? And anyhow is it the right way to work?

General Info of what im trying to do - I intend to access a method on my service that will change/add tasks to the executor. I need it to act as a "listener" up until I don't use the service anymore, thats why I cant just shut it down at any other place rather then the onDestroy().

Sanjay Mohnani
  • 5,947
  • 30
  • 46
sharon gur
  • 343
  • 5
  • 22

1 Answers1

0

The consequences depends on what you are tasks are doing in the executor. If the tasks are not informed about the shutdown, they will keep running as shutdown only ensure that no new tasks can be submitted. What you can do is that you create the executor as daemon. Or you can follow this suggestion for orderly shutdown

The important thing to note is that you tasks should not leave your data in a corrupt state as they can be shutdown anytime. Use transactions if you are updating the database or use appropriate precautions if you are writing to files or sockets.

Community
  • 1
  • 1
ata
  • 8,853
  • 8
  • 42
  • 68
  • so if i set the executor as daemon he will shut himself down when the service dies? considering he was created on the service? And if so will the task that currently working/in queue will still do their job? (they are working on a different thread) – sharon gur Jun 02 '15 at 09:40