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

I’m torn on how to approach the idea of luck. I’m the first to admit that I am one of the luckiest people on the planet. To be born into a prosperous American family in 1960 with smart parents is to start life on third base. The odds against my very existence are astronomical.


I’ve always felt that the luckiest people I know had a talent for recognizing circumstances, not of their own making, that were conducive to a favorable outcome and their ability to quickly take advantage of them.

In other words, dumb luck was just that, it required no awareness on the person’s part, whereas “smart” luck involved awareness followed by action before the circumstances changed.

So, was I “lucky” to be born when I was—nothing I had any control over—and that I came of age just as huge databases and computers were advancing to the point where I could use those tools to write “What Works on Wall Street?” Absolutely.

Was I lucky to start my stock market investments near the peak of interest rates which allowed me to spend the majority of my adult life in a falling rate environment? Yup.