3.9 Android Threads and processes

This tutoria is a brief introduction to Android threads and processes. These two concepts are very important, not only from the point of view of android knowledge but also from the perspective of general programming and computer science knowhow. Thus, this is going to be theoretical session.  Let us not waste time and focuson topic as we have a long way to go. Android Threads

A thread is the smallest unit of processing which can be managed by the CPU scheduler. Generally, threads are contained in a process. Thread is a friendly guy when it comes to point of sharing resources with other threads i.e., threads belonging to same process can share their resources even the operating system resources. A thread has its own ID, a program counter, a register and a stack.


Android Thread

                                            Figure -  Android Thread

You might be thinking what is a program counter, register and stack!! Don’t worry, we will review them.

  • Program Counter: It is a register. It contains the address of the instruction which is currently executed. We can find the next instruction which is going to be executed. After fetching, value of the program counter is incremented by 1 and hence indicates the next instruction.
  • Register: It is a part of computer processor. There are many registers. They provide a small amount of storage and since they are in CPU, access to them is very fast as compared to other storage structures.
  • Stack: It is a data structure, i.e.  It is a way of organizing data in a computer. Storage is in form of pile where “LIFO” pattern is followed, i.e., last in first out architecture is followed.
  • ID: It is the unique ID of a thread which distinguishes it from other threads.

Usually, a thread is contained in a process. There can be multiple threads in a process. Threads facilitate concurrency or multi-tasking in operating systems. For example, Word may have one thread to display graphics and other for checking spelling or grammar mistakes, etc. A process can contain single thread or multiple threads. Let us see the difference between two of them.

Single-threaded process and multi-threaded process

                                          Figure - Single-threaded process and multi-threaded process Advantages of Threads

There are many benefits of threads. Let us check out some of them:

  1. Receptiveness: If an interactive application like a web browser can be multi-threaded then, even if a part of the program is blocked or busy performing any long running task, the application responds to new requests quickly and hence increases the receptiveness of application.
  2. Providence: Threads share resources and hence they are very economical. Allocating memory and resources is just like investing money and thus they reduce the wastage of resources. It is easy to manage threads as compared to processes.
  3. Sharing: Threads share resources by default. They share the code and data and thus applications can share several threads of activity within the same address space.
  4. Architecture: Multi-threaded programming increases the usability of multi-processor architecture.
  5. Distributed Applications: They are very useful in distributed applications as well. For e.g., the server will be multi-threaded one for each client. In fact clients too are multi-threaded, one for managing the connection with server and other for communicating with the server. User Level threads and Kernel-level threads

Thread provides support at two different levels, namely:

  1. User Level Threads: Threads which provide support at user-level are called User-level threads. User threads are supported above the kernel. These threads are supported without kernel support.
  2. Kernel Threads: These threads are supported by the kernel. They are manipulated directly by the operating system. Operating systems like Windows XP, Linux, Solaris, etc support kernel threads.

The relationship between user-level threads and kernel-level threads gives rise to following multi-threading models:

  1. This model maps many user threads to one kernel thread
  2. In user space, thread management is done by thread library. Thus, it is efficient.
  3. There is only one thread which can access the kernel and thus parallelism cannot be expected from multi-processor.

Figure Many-to-one model

                                    Figure - Many-to-one model

  1. There is a kernel thread for each user thread.
  2. It provides concurrency as compared to many-to-one model.
  3. It supports parallelism.
  4. There is an overhead of creating a kernel thread for each user thread. It hinders performance of the application.


Figure - One-to-one model

                                         Figure One-to-one model

  1. In this model, there are smaller or equal numbers of kernel threads with respect to user level threads.
  2. It provides concurrency far better than the one-to-one model.
  3. It supports parallelism.
  4. Developers can create as many threads as required.

Figure - Many-to-many model

                                           Figure - Many-to-many model Thread libraries

Thread library provides an API for creating and managing threads.  There are two ways of implementing thread libraries. They are:

  1. First Approach : Library is entirely provided in the user space. There is no kernel support. Code and data structures of library exist in user space. the function call in library is a local call, not a system call.
  2. Second Approach: Library is implemented at kernel-level. These function calls are implemented at kernel and hence they are system calls, not a local call. Code and data structures for library exist in kernel space.


Figure Approaches to implement thread libraries.

                                                               Figure - Approaches to implement thread libraries.

Nowadays, there are three main thread libraries, namely:

  1. POSIX Pthreads: Pthreads are extension of POSIX standard. They may be provided as user-level library or kernel-level library.
  2. Java: Java thread API allows thread creation and management in the Java programs directly. JVM runs over host operating system. Thus are implemented using thread library present at host system.
  3. Win32: This library is a kernel-level library. These are available on Windows operating system.

Java threads are implemented on windows using Win32 API. These are implemented on UNIX and LINUX systems using Pthreads.


Figure Thread libraries

                                                                   Figure - Thread libraries Threads in Android

The reason behind this whole discussion was because of the fact that threads are an inevitable part of android development.

  • In every project we have a main and this main is our application thread. Whenever an application is launched main thread is created. This thread is responsible for dispatching all the application components, including widgets and views.
  • The main thread is also known as UI thread.
  • Separate threads are not created for each instance of component.
  • Components belonging to the same process are instantiated by this main thread.
  • Procedures which prick call back methods are run on this thread.
  • For example, when you touch checkbox, Application or app’s UI thread dispatches touch event to the widget. 
  • When we are performing long running tasks, the thread must be very well managed otherwise it would take too long for processing. If thread is blocked then it may result in ANR or Application Not Responding error i.e., our app hangs.
  • Background thread must never block UI thread as all manipulations are to be done from UI.
  • There are two rules while programming :
  1. UI thread should not be blocked
  2. Android UI toolkit should only be accessed by UI thread. No other thread should try to access it.

We shall study about threads in detail in upcoming chapters. Now let us move to next topic i.e., Processes.