Hello Readers, In this blog, we will understand what is Multithreading In Java, the Life cycle of a thread, Thread Class, Thread using a Runnable Interface:
What is Multithreading
- Multithreading is the process of creating multiple threads for executing multiple independent tasks concurrently to complete their execution in less time by using CPU ideal time effectively.
- All tasks are executed in a single process.
- Multithreading is lightweight because switching between contexts is fast because all threads are created in a single process means threads are created at the same address.
Life Cycle of a Thread :
There are five states a thread has to go through in its life cycle. This life cycle is controlled by JVM (Java Virtual Machine). These states are:
- new = In this state, a new thread begins its life cycle. This is also called a born thread. The thread is in the new state if you create an instance of the Thread class but before the invocation of the start() method.
- Runnable = A thread becomes runnable after a newly born thread is started. In this state, a thread would be executing its task.
- Running = When the thread scheduler selects the thread then, that thread would be in a running state.
- Non-Runnable (Blocked) = The thread is still alive in this state, but currently, it is not eligible to run.
- Terminated =A thread is terminated due to the following reasons:
- Either its run() method exits normally, i.e., the thread’s code has executed the program.
- Or due to some unusual errors like segmentation fault or an unhandled exception.
What is the use of the main thread and garbage collector thread?
- main thread is used for executing java methods logic by providing a separate memory block in it. This memory block is technically called a stack frame. With many methods and constructors, we invoke those many new stack frames are created. Once the current method and constructor logic are executed, the stack frame is destroyed.
- Garbage Collector is used for destroying unreferenced objects from the heap area, freeing this memory, and returning to JVM. So that JVM will use this free memory for creating new objects.
What are the different ways we have to create a custom thread in java?
We can create a custom thread in two ways:
- Extending from Thread class.
- Implementing from Runnable Interface.
Extending from Thread class:
Creating a custom thread extending from the Thread class is a three steps process:
- 1) Create a class deriving from Thread class i.e,
- 2) Overriding run() method and place the logic that we want to execute from this custom thread
- 3) Create this subclass object and invoke the start() method using this subclass object.
Save this program with the name MyThread.java and then compile and execute.
Implementing from Runnable Interface.
Creating custom thread implementation from the Runnable interface is a four steps process:
- 1)Create a class deriving from the Runnable Interface
- 2) Implement the run() method and place the logic that we want to execute from this custom thread.
- 3) Create this subclass object.
- 4) And create the Thread class object explicitly bypassing this Runnable subclass object as an argument, then invoke the start() method using the Thread class object.
Save this program with the name MyRunnable.java and then compile and execute and observe the output.
So, this was the basic understanding of Multithreading in Java. This brings us to the end of this blog. Hope this helped you understand Multithreading in Java better.