1

I was reading in the Web (Why to use Service if it runs in the same thread in android) about Services and Threads and i came to a conclusion that in general Services have a higher priority than Thread.

I have an app that executes a download operation using simple Threads via a static class and completes in 5 minutes. But when i press Home button and after a while open the app again the execution went slower because i pressed the home button.

What am i asking is, if i use a service will go faster or by pressing the home button it is normal to slow down the apps or priorities are irrelevant all together with the execution time?

Thank you.

Nick
  • 2,818
  • 5
  • 42
  • 60

1 Answers1

1

No, they don't. Service aren't an execution context, they don't have a priority at all. I'm not sure where you got that idea from, but its like saying peanuts have a higher priority than volleyball. It just doesn't make sense.

Now whatever an app is in the foreground will generally get the CPU first, but downloading isn't a CPU intensive operation. Its network intensive. You'll have no problem getting the CPU long enough to read from the socket, even if backgrounded.

The point of a Service is that it provides a Context, yet isn't tied to any UI, any screen of your app, or your app being in the foreground. Its a place you can do background processing, or process data needed by multiple Activities. That's it, it has nothing to do with threading or priorities.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Its not only downloading, i also write to a database in Room. So why execution speed slows down in the background? If i clearly understand, this is normal for any app right? The download link i provided says that in an documented answer. – Nick Sep 13 '18 at 18:17
  • 1
    It has a lot to do with the intricacies of the Linux scheduler, but basically the OS tries to optimize to give the most CPU time to the most interactive application. That will almost always be the app in the foreground (and if it isn't, there's probably something wrong) so that your device stays as responsive as possible. However if it has free CPU time it will schedule your app – Gabe Sechan Sep 13 '18 at 18:20
  • OKK. So using Threads or Services doesn't make any difference at speed whether its in the foreground or background. Right? Thank you – Nick Sep 13 '18 at 18:22
  • 1
    Right. In the linked post, when you see priority, its talking about priority for when ANdroid kills processes to free up memory. Background Activities have the lowest (killed first). Foreground services have the highest (killed last), except for the foreground Activity (will not be killed unless you go completely OOM). – Gabe Sechan Sep 13 '18 at 18:23
  • One last question Gabe. If i use a foreground service (to download and insert to databse )and a notification channel, even if i press the home button to the activity, wouldn't that keep the execution speed normal as if activity was running in the foreground? (because the download is visible in the notification channel) – Nick Sep 14 '18 at 10:23
  • It depends on what else is going on in the phone. Best case would be an idle screen like the home screen. Then you'll be at more or less the same speed. Worst case would be something like the user kicking off a game. Then you'll have lots of competition for the CPU. – Gabe Sechan Sep 14 '18 at 13:24
  • Yeah but a process/app with a foreground service will be definitely faster than a background process/app (home button pressed). So there is a difference when using a foreground service right? – Nick Sep 14 '18 at 13:31
  • 1
    No, they'll be the same. A foreground service is less likely to be killed for resources than a background service, and isn't going to be limited in how long its allowed to execute on newer devices. Now if you think your process is going to take a while, make it a foreground service so it isn't killed. – Gabe Sechan Sep 14 '18 at 13:33