Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application threads vs Service threads

What are the advantages/disadvantages in placing a lengthy network access code in a thread in an activity or a thread in a service? How would it affect the application? I am writing a streaming audio player and from what I've read so far putting the code in a service will still end up blocking the application so a new thread is needed, does anyone know if it makes more sense to put this piece of code in a service.

like image 555
zerayaqob Avatar asked Apr 13 '10 21:04

zerayaqob


People also ask

What are the differences between service and thread?

Service : is a component of android which performs long running operation in background, mostly with out having UI. Thread : is a O.S level feature that allow you to do some operation in the background.

What is an application thread?

An application consists of one or more processes. A process, in the simplest terms, is an executing program. One or more threads run in the context of the process. A thread is the basic unit to which the operating system allocates processor time.

What's the difference between threads and processes?

A process is a program under execution i.e an active program. A thread is a lightweight process that can be managed independently by a scheduler. Processes require more time for context switching as they are more heavy. Threads require less time for context switching as they are lighter than processes.


2 Answers

Yes, a blocking operation in a Service will still block the application. Despite first appearances, Services are not simply for running tasks in the background. They are for running tasks with a lifecycle that is independent of the Activity lifecycle (IE, they may continue after the Activity is closed).

A Service that starts when an Activity starts and ends when the Activity ends is useless.

In your case, where you are streaming audio, you may want to stream audio even after the user closes the Activity, in which case, you should use a Service, but you'll still need a thread (or an AsyncTask) for blocking tasks.

like image 107
synic Avatar answered Oct 16 '22 21:10

synic


From my experience (1+ years developing Android), there is no difference between running a new thread in a service or in an activity. Try not to keep a reference to the Activity in the new thread - use the application context.

Also, the service's life-cycle didn't help at all because some methods are not guaranteed to be invoked :(

The only difference may be that the service can be destroyed without destroying the app completely - thus potentially destroying the new threads. Why potentially? because on the practical side, this doesn't happen. The app ALWAYS gets killed without killing the service before that, meaning: the local service mechanism is useless!!!

Remote service is a different discussion - I was referring only to "where should I run a new thread?".

Good luck!!!

like image 37
AlikElzin-kilaka Avatar answered Oct 16 '22 21:10

AlikElzin-kilaka