Javatpoint Logo
Javatpoint Logo

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.

Thread Concept in Java

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:

  • Feature through which we can perform multiple activities within a single process.
  • Lightweight process.
  • Series of executed statements.
  • Nested sequence of method calls.

Thread Model

Just like a process, a thread exists in several states. These states are as follows:

Thread Concept in Java

1) New (Ready to run)

A thread is in New when it gets CPU time.

2) Running

A thread is in a Running state when it is under execution.

3) Suspended

A thread is in the Suspended state when it is temporarily inactive or under execution.

4) Blocked

A thread is in the Blocked state when it is waiting for resources.

5) Terminated

A thread comes in this state when at any given time, it halts its execution immediately.

Creating Thread

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:

Thread Class

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.

  • Thread()
  • Thread(Runnable, String name)
  • Thread(Runnable target)
  • Thread(ThreadGroup group, Runnable target, String name)
  • Thread(ThreadGroup group, Runnable target)
  • Thread(ThreadGroup group, String name)
  • Thread(ThreadGroup group, Runnable target, String name, long stackSize)

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.

start() method

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:

ThreadExample1.java

Output:

Thread Concept in Java

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.

ThreadExample2.java

Output:

Thread Concept in Java

In the above example, we perform the Multithreading by implementing the runnable interface. To learn more about Multithreading, click here.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA