Multithreading in Java is a very important concept to learn. Firstly, I would recommend reading Instance vs Object vs Reference.
Before starting, let me ask you a question What is Multitasking? – Doing multiple tasks at a time right?
Similarly, multithreading in java is a process of executing multiple threads simultaneously. Let’s deep dive into the concept.
1. What is Multitasking?
In computer programming, Multitasking is a process of executing multiple tasks concurrently in order to achieve high CPU utilization. Multitasking environment utilises computer hardware efficiently and it can be achieved in two ways:
- Process-Based Multitasking (Multiprocessing)
- Thread Based Multitasking (Multithreading)
1.1 Process-Based Multitasking:
Process-based multitasking is also known as Multiprocessing. All your day-to-day computer routine falls under this category like listening to songs, rendering video, and browsing the internet at the same time.
- Best suitable at the OS level.
- Heavyweight process.
- It happens with multiple programs.
- Each process executes at a separate memory location.
1.2 Thread Based Multitasking:
Thread based multitasking is also known as Multithreading. It happens at the code level, that means you can achieve multithreading in java while writing a single Java Program.
- Best suitable at the programming level.
- Lightweight process.
- Multithreading occurs in a single program.
- All Threads use the same memory address because of a single process.
2. What is Thread?
As we discussed, multithreading in java happens at the code level in a single program. Basically, a thread is the smallest part of any program/process.
In layman’s terms, a thread is a light weighted subprocess of any program that runs simultaneously with other threads.
- Thread is lightweight.
- Thread is independent.
- All threads share the same memory.
3. What is Multithreading in Java?
As we discussed, a thread is a subprocess. In multithreading, multiple threads are executed simultaneously to perform any task.
If your application is using multithreading, that means two or more threads are running concurrently to save time and memory.
In a multithreaded application, all threads use same memory location, in result to save memory.
Important: One thread is executed at a time in a multithreaded environment. Then why it is termed multithreading?
Because context switching between threads is very fast. Also, if you’re having quad-core machine then 4 threads can be executed parallelly.
Response time will be increased in a multithreaded environment because every single thread is independent and will not wait for any other thread to complete.
- It saves time and memory.
- Better utilisation of computer resource.
- Context switching between threads is possible.
4. Thread Lifecycle in Java
Below is the lifecycle of a Thread.
- NEW – A thread that has not yet started.
- RUNNABLE – A thread executing in the Java virtual machine.
- BLOCKED – If a thread is blocked because of any other thread.
- WAITING – A thread that is waiting indefinitely for another thread.
- TIMED_WAITING – A thread that is waiting for another thread up to a specified waiting time.
- TERMINATED – A thread that has exited.
Remember: A thread can be only in one state at a time.
I hope you understand the basic concept of Multithreading in Java. Follow Next Lesson in order to learn the creation of a Thread.
Official Docs: Visit Here