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.
18.104.22.168 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.
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.
Figure - Single-threaded process and multi-threaded process
22.214.171.124 Advantages of Threads
There are many benefits of threads. Let us check out some of them:
- 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.
- 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.
- 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.
- Architecture: Multi-threaded programming increases the usability of multi-processor architecture.
- 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.
126.96.36.199 User Level threads and Kernel-level threads
Thread provides support at two different levels, namely:
- 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.
- 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:
- MANY TO ONE MODEL;
- This model maps many user threads to one kernel thread
- In user space, thread management is done by thread library. Thus, it is efficient.
- There is only one thread which can access the kernel and thus parallelism cannot be expected from multi-processor.
Figure - Many-to-one model
- ONE-TO-ONE MODEL
- There is a kernel thread for each user thread.
- It provides concurrency as compared to many-to-one model.
- It supports parallelism.
- There is an overhead of creating a kernel thread for each user thread. It hinders performance of the application.
Figure One-to-one model
- MANY TO MANY MODEL
- In this model, there are smaller or equal numbers of kernel threads with respect to user level threads.
- It provides concurrency far better than the one-to-one model.
- It supports parallelism.
- Developers can create as many threads as required.
Figure - Many-to-many model
188.8.131.52 Thread libraries
Thread library provides an API for creating and managing threads. There are two ways of implementing thread libraries. They are:
- 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.
- 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.
Nowadays, there are three main thread libraries, namely:
- POSIX Pthreads: Pthreads are extension of POSIX standard. They may be provided as user-level library or kernel-level library.
- 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.
- 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
184.108.40.206 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 :
- UI thread should not be blocked
- 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.