Java: Beginner Guide to Multithreading

a thread...

Multithreading is a concept of applying multitasking in Java.

Java supports thread-based multitasking.

Java program can be divided into several threads and those threads can be executed in parallel to support multi-tasking.
Two ways to create a thread in Java:

1. By Implementing Runnable Interface
2. By extending the Thread class
Let's create a thread by implementing the "Runnable" interface:

Steps:
1. Implement Runnable Interface
2. Override the run() method, and put your code inside it.
3. Pass the instance of your class to the Thread class constructor.
4. Call the start() method to run your thread.
Let's create a thread by extending the "Thread" class:

Steps:
1. Extend your class with Thread class
2. Override the run() method, and put your code inside it.
3. Instantiate your class.
4. Call the start() method to run your thread.
Out of the above approaches, it is always advisable to use the first approach(By implementing a Runnable Interface) to create the threads in Java.

Because it's an Interface, you can also extend other classes in the future and also implement other interfaces.
Lifecycle of thread:

It has 5 different phases in its lifecycle:
1. New
2. Runnable
3. Running
4. Waiting
5. Dead
Few Important Methods of Thread Class:

run() - Actual task of the thread is defined here.
start() - Starts the thread
join() - Wait for thread to die.
setName() - Give name to our thread.
getName() - Returns the thread name.
setPriority() - Sets the priority to thread.
getPriority() - Returns the priority.
getState() - Returns the state of thread.
isAlive() - checks if thread is alive or not

and a few more...
Conclusion:

We can use Multithreading in order to boost the performance of our program.

If our program has independent units, we can run those independent code blocks into a separate thread.

They can run parallel and process faster than normal.

More from Vikas Rajput

You May Also Like

The chorus of this song uses the shlokas taken from Sundarkand of Ramayana.

It is a series of Sanskrit shlokas recited by Jambavant to Hanuman to remind Him of his true potential.

1. धीवर प्रसार शौर्य भरा: The brave persevering one, your bravery is taking you forward.


2. उतसारा स्थिरा घम्भीरा: The one who is leaping higher and higher, who is firm and stable and seriously determined.

3. ुग्रामा असामा शौर्या भावा: He is strong, and without an equal in the ability/mentality to fight

4. रौद्रमा नवा भीतिर्मा: His anger will cause new fears in his foes.

5.विजिटरीपुरु धीरधारा, कलोथरा शिखरा कठोरा: This is a complex expression seen only in Indic language poetry. The poet is stating that Shivudu is experiencing the intensity of climbing a tough peak, and likening

it to the feeling in a hard battle, when you see your enemy defeated, and blood flowing like a rivulet. This is classical Veera rasa.

6.कुलकु थारथिलीथा गम्भीरा, जाया विराट वीरा: His rough body itself is like a sharp weapon (because he is determined to win). Hail this complete

hero of the world.

7.विलयगागनथाला भिकारा, गरज्जद्धरा गारा: The hero is destructive in the air/sky as well (because he can leap at an enemy from a great height). He can defeat the enemy (simply) with his fearsome roar of war.