Hello reader!!
In this blog we'll see about multithreading in java. At the end of this blog you have a strong understanding about threads and you can able to create your own thread.
Content
1. What is a Thread and Multi-thread?
2. Multi-task vs Multi-thread ?
3. Thread Life cycle
4. Thread Implementation
- Inheritance
- Interface
5. Advantages of multi-threading
1. What is a Thread and Multi-thread?
Thread is a light-weight sub process or a small unit of process in a program. Each program has atleast one thread known as main-thread which is created by the jvm (Java Virtual Enviroment) at the run time of a program.
Multi-Thread is simply more than one thread. Java has a feature that allows concurrent execution of two or more parts of a program to make more efficient.
2. Multi-task vs Multi-thread
Multi-tasking | Multi-threading |
- Execution of a running program. | - Execution of an indivual part of a program. |
- Each task has its own address. | - All threads shares a common address. |
- Heavy-weight process | - Light-weight process |
- Controlled by the operating system. | - Controlled by JVM. |
3. Thread Life cycle
Each and every thread has five states, which is controlled by the jvm.
Thread states as follows:
1. New
Whenever a instance of a thread is created,, it is said to be in a new state,
Before the invocation of start() method.
2. Runnable
After invocation of start() method, the thread is said to be in a runnable state,
But the thread scheduler has not selected it to be the running thread.
3. Running
If the thread scheduler has selected any thread for the execution then it is said to be in a running state.
4. Blocked/Wait
When the thread is currently inavctive but still alive, it is said to be in a blocked or waiting state.
If any thread is waiting for i/o operation, it falls to this state.
5. Terminated
when the code of thread has entirely executed or there occurred any unusual erroneous event, like segmentation fault or an unhandled exception, the thread falls to a teriminated or dead state.
4. Thread Implementation
A thread can be implement by creating an instance of thread from Thread class
.
Two Mainly used methods for thread creation follows ,
- start() is used to start a newly created thread. It performs following tasks:
- run() is used to perform action for a thread.
4.1 Inheritance
By
extending the thread class
, butwe can't extend any other classes
.
Class Example extends Thread{ public void run(){ System.out.println("Running successfully"); } } Class Main{ public static void main(String args[]){ Example ex=new Example(); ex.start(); } }
Output: Running successfully
4.2 Interface
By
implementing the runnable class
and passing it tothread class
.
class Example implements Runnable{ public void run(){ System.out.println("Thread is running by implementing runnable..."); } public static void main(String args[]){ Example ex=new Example(); Thread t =new Thread(ex); t.start(); } }
Output: Thread is running by implementing runnable...
5. Advantages of multi-threading
- It doesn’t block the user because threads are independent and you can perform multiple operations at same time.
- Time efficient.
- Threads are independent so it doesn’t affect other threads if exception occur in a single thread.
Thats it ! Hope It helped. Thanks for reading. Stay tuned!!.