![]() |
VOOZH | about |
Working on multiple tasks at the same time is Multitasking. In the same way, multiple threads running at the same time in a machine is called Multi-Threading. Technically, a thread is a unit of a process. Multiple such threads combine to form a process. This means when a process is broken, the equivalent number of threads are available.
For example, Autocorrect is the process where the software looks for the mistakes in the current word being typed. Endlessly checking for the mistake and providing suggestions at the same time is an example of a Multi-Threaded process.
Let's try to visualize Multi-Threading with the help of an Android App. In the below example, 3 Threads start at the same time on a button click and work concurrently.
Approach:
Step 1: Add the below code in activity_main.xml. Here we add three TextViews and a button.
Step 2: Add the below code in MainActivity. Here, three threads are made, each thread keeps updating the respective TextView every second (declared in the code) when the button is clicked. These Threads keep running until the button is clicked again (i.e."Stop").
Output:
Multi-Threading concept applies to a large number of day to day applications on various machines. However, there are drawbacks to the same. At the beginner level, one can only think of the load on the machine or increased complexity. But at a greater level, multiple scenarios such as complex testing, unpredictable results, deadlock, starvation, etc are expected.