Android

Process And Threads In Android

Process is an Operating System’s creation for executing instructions either from operating user or system itself. Any task of OS (Operating System) is executed as a process. Creating a process is one of the main functionalities of an OS. For these reasons, it has been measured as a basic unit of CPU execution.

Every task execution in a computational system can be viewed as process at OS level. The main functionalities of a process are following,

  • Isolation
  • Virtualisation

On the event of process creation, OS creates an isolated environment with virtual resources i.e, restricted access/usage to the resources like CPU, Memory, Network, Network, Power and I/O. This virtual concept of OS is collectively identified as a Process.

Each process in OS has unique identifier that is Process ID. It is again mapped to the creator/ current user of the OS.

In the context of Android, it is the Linux OS underlying at the core. So the process management is exactly similar to what in Linux OS.

Android is a Linux operating system but on the top of that, is has multiple levels of abstraction (there is a reason for this architecture will discuss in an another occasion) till it reaches to the core Linux OS part. A task in Android is going through these intermediate software units as a request-response model through multiple interfacing systems in the Android framework. Once it reaches at the core, rest of the execution flow is same as i explained above.

Please note: An Android application is a collection of 4 components, that are, Activities, Services, Broadcasts, Content Providers. Execution of any of these components will result in a process creation by OS

When an application starts and it does not have any other components (Activities, Services, Broadcasts, Content Providers) running, the Android operating system creates a new Linux process for the application with a single thread of execution. That is a single process with a single thread.

As you know, Android operating system kernel (the core) is of Linux. So each process running on Android has been treated exactly same as the one in Linux operating system.

Each process has an isolated environment within it has its own resources (CPU, memory, network etc) allocated to perform its tasks. Each process is treated in Linux Operating system as a user. Each process can be identified by a Linux user id.

Please note: You can see the list of processes running in a device through Device monitor tab in Android Studio.

By default, all components of the same application run in the same process and thread, called the “main” thread. A Application can start its components in another process also.

By default, all components of the same application run in the same process.

The manifest entry for each type of component element—<activity>, <service>, <receiver>, and <provider>—supports an android:process attribute that can specify a process in which that component should run.

You can set this attribute so that each component runs in its own process or so that some components share a process while others do not.

You can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.

Please note: Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Application components running in the process that’s killed are consequently destroyed. A process is started again for those components when there’s again work for them to do.

In multi-threaded environment we separate execution from main thread to different threads for better CPU utilisation.

Thread

Thread is a basic unit of CPU execution. Thread has a thread ID, program counter, a register set, and a stack similar to a process.

The default thread that creates UI is known as UI thread or main thread. All other threads are known as Worker threads.

When an application is launched, the system creates main/UI thread of execution for the application. This thread is in charge of dispatching events to the appropriate user interface widgets. It is also almost always the thread in which your application interacts with Android UI. As such, the main thread is also sometimes called the UI thread.

The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread or main thread, and system calls to each component are dispatched from that thread.

Please note: UI callbacks are always run in the UI thread of the process.

For instance, when the user touches a button on the screen, your app’s UI thread dispatches the touch event to the widget, which in turn sets its pressed state and posts an invalidate request to the event queue. The UI thread dequeues the request and notifies the widget that it should redraw itself.

When your app performs intensive work in response to user interaction, this single thread execution result in poor performance.

Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. From the user’s perspective, the application appears to hang.

Please note: Even worse, if the UI thread is blocked for more than a 5 seconds then application will be closed by the system forcefully.

Additionally, the Android UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread. Thus, there are simply two rules to Android’s single thread model:

  • Do not block the UI thread.
  • Do not access the Android UI from outside the UI thread.

Multithreading

Multithreading is the execution of machine codes in multiple threads rather than in single thread. The main purpose of Multithreading is better CPU utilisation for each process. A process can create any number of threads and one thread can create another thread as well.

As i said above, a stream of execution without a delay or wait, only such code can be executed in main thread and if any asynchronous, that is not sequential, requires changing the main thread track and starts a parallel execution using worker thread or background thread.

Please note: Stack memory in Virtual machine is only accessible inside a thread. That means the data stored in stack memory is not accessible outside that thread. That makes the section of the code stored in the stack memory thread safe. While heap memory is a shared memory that can be accessible across all the threads in a process.

About author

Rojer is a programmer by profession, but he likes to research new things and is also interested in writing. Devdeeds is his blog, where he writes all the blog posts related to technology, gadgets, mobile apps, games, and related content.

Leave a Reply

Your email address will not be published. Required fields are marked *