Service, Thread and Process are terms that may cause confusion for many people who do programming in Android or similar platforms. I’m going to explain here what they are and the difference between the m in simple words. I assume you already know the Android application components like activity, service ,….
Let’s start by some basic definitions ,,
In general, You can define process as a program unit of execution that has its own allocated resources and memory. Running programs usually consists of multiple processes (one main controller process and many childs) . Multiple processes are independent of each others and can communicate only via Inter Process Communication mechanisms that are provided by the hosting operating system.
Aside: Have you heard that “Google Chrome” runs the tabs of the browser each in different process ? . By doing this, They provide isolation between the tabs handling web pages so each tab has it’s own resources and handling mechanisms. If one crashed, the others are still Ok. Because they are independent 😉
Thread is part of your program functionality that can run in parallel with the main application execution process (or thread). It runs withing a process. The process may contain only, and that’s rare, one thread and mostly contains multiple threads. Threads share the same resources and memory. Creating new thread need very less work that creating a new process.
Example: Multiple downloads that are running in the same time in a file downloader software. And you may ask here , but if one download crashes, the others are not!!. Yes. I didn’t say threads are dependent of each others. I said they have the same resources allocated.
That was the difference between thread and process in general, lets go some inside Android.
When an application’s first component starts , the Android system starts a new process for the application with a single thread of execution called “main” thread or “UI Thread“. This thread responsible for very important stuff like dispatching events to the user interface widgets. The started Process then may hold Activity, Service or BroadCastReceiver.
All components of the same application usually run in the same process. When new component is started, it will , by default, run in the same thread and inside the application main process unless you provide some thing else. For example if you run a service by startService() method from an activity, this service runs in the main application process and thread. This is called “Single Thread Model”.
In this case (running service from an activity to do some intensive work), this single thread model can cause poor performance and may cause your application not responsive and may also see the “ANR” dialog.
Here comes the need for creating worker thread(s) that runs in background in parallel with the “main” thread leaving the main “UI” thread responsive. This is a very common technique that you will do every day when developing Android apps.
Building Worker Threads in Android
Building worker threads can be done in several ways:
– Implementing Thread using the Java style ( Extending Thread class or Implementing runnable interface )
– In case we are working with services, You may use IntentService instead of Service class.
– For the problem of handling and updating user interface from different threads , the Android SDK is shipped with many ready solution for that like :
– Using AsyncTask
– Using the view.post method
….. and many others.
We may explore those solutions later in upcoming tutorials.
kindly leave you comments, corrections, additions or suggestions below .