0

Why does a service not run in it's own thread under Android? And what were the design considerations?

Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
powder366
  • 4,351
  • 7
  • 47
  • 79

3 Answers3

1

It is all about Flexibility: I assume you are familiar with so many Frameworks like for example e.g. Volley, Retrofit etc. These Frameworks has their own Thread implementation behind scene so if Service by default is executed in a separate Thread then it is overkill. Why? because the threading is handled by the framework already so why the need to have another thread?

See also Why is creating a Thread said to be expensive?. And put also into consideration that you are doing Threading in an Android device and not into a powerful Desktop.

Enzokie
  • 7,365
  • 6
  • 33
  • 39
0

Take a look at IntentService I think this is that you are looking for.

upenpat
  • 685
  • 3
  • 12
  • How come did they design it this way (I know IntentService)? – powder366 May 30 '14 at 09:39
  • suppose you want to do some background work which is not going to consume too much of CPU time then rather than doing that work in a new thread,you can do it in main thread of your app(with less/no performance degradation). But if u were to something CPU intensive work then doing that in main thread of your application will slow down your application so for that you create a new thread and do the work in the new thread. – upenpat May 30 '14 at 09:48
0

It gives you more flexibility. For example you could run each request in a seperate thread with special priorities or you could store the requests in a queue and only start the next thread if the previous one has finished. A IntentService provides a default configuration that in the most cases fits your needs from scratch.(http://developer.android.com/guide/components/services.html). Such decisions depend on your use case.

Patrick Leitermann
  • 2,144
  • 2
  • 13
  • 13