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.
Sample Android App:
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.
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”).
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- ParticleView in Android with Examples
- ShimmerLayout in Android with Examples
- Android | What is Toast and How to use it with Examples
- SoundPool in Android with Examples
- Screen Orientations in Android with Examples
- Android | Horizontal RecyclerView with Examples
- BubbleEmitter animation in Android with Examples
- Dynamic CheckBox in Android with Examples
- Shared Preferences in Android with Examples
- Modal Bottom Sheet in Android with Examples
- Different ways to hide Action bar in Android with Examples
- Android | Implicit and Explicit Intents with Examples
- Difference between Java and Kotlin in Android with Examples
- TextView widget in Android using Java with Examples
- EditText widget in Android using Java with Examples
- Firebase RealTime Database with Operations in Android with Examples
- How to install Android Applications in Mobile Phone without USB Cables using Android Studio
- How to implement Swipe Down to Refresh in Android using Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Multithreading in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.