Thread Concept in Java
Before introducing the thread concept, we were unable to run more than one task in parallel. It was a drawback, and to remove that drawback, Thread Concept was introduced.
A Thread is a very light-weighted process, or we can say the smallest part of the process that allows a program to operate more efficiently by running multiple tasks simultaneously.
In order to perform complicated tasks in the background, we used the Thread concept in Java. All the tasks are executed without affecting the main program. In a program or process, all the threads have their own separate path for execution, so each thread of a process is independent.
Another benefit of using thread is that if a thread gets an exception or an error at the time of its execution, it doesn't affect the execution of the other threads. All the threads share a common memory and have their own stack, local variables and program counter. When multiple threads are executed in parallel at the same time, this process is known as Multithreading.
In a simple way, a Thread is a:
Just like a process, a thread exists in several states. These states are as follows:
1) New (Ready to run)
A thread is in New when it gets CPU time.
A thread is in a Running state when it is under execution.
A thread is in the Suspended state when it is temporarily inactive or under execution.
A thread is in the Blocked state when it is waiting for resources.
A thread comes in this state when at any given time, it halts its execution immediately.
A thread is created either by "creating or implementing" the Runnable Interface or by extending the Thread class. These are the only two ways through which we can create a thread.
Let's dive into details of both these way of creating a thread:
A Thread class has several methods and constructors which allow us to perform various operations on a thread. The Thread class extends the Object class. The Object class implements the Runnable interface. The thread class has the following constructors that are used to perform various operations.
Runnable Interface(run() method)
The Runnable interface is required to be implemented by that class whose instances are intended to be executed by a thread. The runnable interface gives us the run() method to perform an action for the thread.
The method is used for starting a thread that we have newly created. It starts a new thread with a new callstack. After executing the start() method, the thread changes the state from New to Runnable. It executes the run() method when the thread gets the correct time to execute it.
Let's take an example to understand how we can create a Java thread by extending the Thread class:
Creating thread by implementing the runnable interface
In Java, we can also create a thread by implementing the runnable interface. The runnable interface provides us both the run() method and the start() method.
Let's takes an example to understand how we can create, start and run the thread using the runnable interface.
In the above example, we perform the Multithreading by implementing the runnable interface. To learn more about Multithreading, click here.
Next TopicUpcasting and Downcasting in Java