Threads and Classes in android applications
Threads and task classes have been essential parts of computer programming for decades. Most modern programs, including android, operate and support threads. This enables programmers to have sections of their programs executed in parallel. The programs are spilt into units which are executed simultaneously.
Threads
A thread is the smallest executable unit of program processing that can be scheduled by the operating system. Programs split into executable threads are versatile and can run more efficiently. For instance a program with sections that require different execution times can be ran in different threads; this allows the main UI program to remain quick and responsive.
Multi-Threading and Classes
The most unique feature in Android is multi-threading where multiple features are executed together exclusively. Android multi-threading is more or less like the conventional multi-threading in computer applications.
A class on the other hand can be thought of as a process that has its methods split into threads. All the methods in a given class can be executed concurrently. In android, multi-threading is easily achieved through use of any of its many native classes. Out of these inbuilt classes, handler class is used more often.
Handler class in Android
Handler classes are inbuilt in the handler package of android OS. It is the most commonly used class for multi-threading where it provides features for sending and receiving messages among different threads. They are also vital in the handling the execution of threads associated with handler classes.
Every thread in an android class is associated with a handler class instance and it allows threads to run together harmoniously while communicating amongst them through messages
Instantiating Handler class
A handler class can be instantiated to enable multi-threading. This is done through either Default constructor or through parameterized constructor.
Why Processes and threads
Every time an application is launched, android system creates a thread for execution called “main”. This process mainly initiates the dispatch and display of events such as widgets and other android UI tools. As such, the main thread is called the UI thread. All components that running in the same process are initiated in this thread. As such, methods that require response from system callbacks are initiated in the main thread. For example, when a button is touched on the screen, the app’s UI sends the touch event onto the app’s widget which dispatches onto the event queues.
However, when an app performs intensive work such as network access or database queries, this single UI thread yields poor performance. The long operations will block the main UI making the impression from the user’s perspective that the application has hang. Worse still, an application blocked for more that 5 seconds presents the infamous error ”application not responding” making the user very unhappy.
Worker threads
The single thread model therefore presents operational problems which android tries to solve by use of threads