Should I use setDaemon () in android?

I am creating a Thread service for my application, this thread will perform background tasks, and therefore it will only be used if my main thread is running.

So should I declare him a demon?

+6
source share
2 answers

On Android, it’s better to make sure that you explicitly control your threads. Let them know when to stop.

See related discussion here. They found no solution and instead observed long-lived streams:

What hooks do we have to complete the workflow when the application exits

Please note that standard Java shutdown bytes are not guaranteed on this platform:

http://developer.android.com/reference/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29

So, instead of relying on the (undocumented?) Belief that Android will kill your virtual machine anyway and guess about the daemon / not daemon, it seems better to control the flows.

+3
source

Not really. Android does not have main() methods for applications, and they do not exit, but are managed by the system. If he decides to kill your application for free resources, he (most likely) doesn't care if you have daemon streams or not.

+2
source

Source: https://habr.com/ru/post/927915/


All Articles