Explain Thread in Java

 Thread in Java 

1.What is thread in java 

2.Creating thread in java 

3.Life cycle of thread

4. Method of Thread Class

=> What is thread

In Java, a thread is the course or path followed while a programme is being run. The main thread, which is typically present in all programmes, is provided by the JVM, or Java Virtual Machine, at the beginning of the program's execution. The main() method is now called by the main thread when it is given control of the main thread.

A program's execution thread is known as a thread. An programme running on the Java Virtual Machine can execute several threads at once. Every thread has a different priority. Priority order determines which threads are run first.

Because it allows for the execution of numerous operations within a single procedure, thread is essential to the programme. There are frequently separate programme counters, stacks, and local variables for each thread in the programme.

=>Creating a thread in Java

Let's learn how threads are produced in Java now that we are familiar with its whole thread lifecycle. As we've seen, Java offers two different ways to create threads.Extending the Thread class is the first option. In this procedure, a child class that extends java.lang is created.thread type. The Runnable interface implementation is the alternative. A class implements the java.lang during this procedure.interface for runnable.

>Extended java.lang.Thread class

In this instance, a thread is formed by a new class that extends the Thread class, creating an instance of that class, and implementing the functionality of the Thread in the run() method.

Let's take an example program

package test;
public class My Thread extends Thread {
public void run () 
{
System.out.println("thread is running ....");
}
public static void main(String [] args)
{
My Thread  obj = new My Thread(); 
obj.start();
}
}

>Implementing Runnable Interface 

This is the simplest way to link the two together. In this instance, the run() method and the runnable interface are implemented by a class.

Always write your thread execution code inside of the run() method.

Let's take an example program

package test;
public class My Thread implements Runnable {
public void run () 
{
System.out.println("thread is running ....");
}
public static void main(String [] args)
{
Thread t  = new Thread(new my Thread());
obj.start();
}
}

=>Lifecycle of a Thread in Java

The Java term "Life Cycle of a Thread" describes the changes in a thread's state between the time of its birth and when it dies. The thread enters the runnable state after being created and run by invoking the start() function of the Thread class. The thread enters a non-runnable phase when the sleep() or wait() methods of the Thread class are used.

Statement execution begins when the thread transitions from the non-runnable state to the runnable state. After leaving the run() process, the thread is killed. These changes to the thread state are known as the thread life cycle in Java.

There are 4 stages in the lifecycle of Thread in java

1. New
2. Runnable 
3. Running 
4. Blocked (not runnable state)
5. Dead


New state 

The thread is created and declared as being in the New state as we use the Thread class to establish it. In other words, a thread enters a new state when it is created, but the start() method on the instance has not yet been called.

Runnable state 

The thread that will run the code is already in the runnable state. A new thread enters a runnable state when the start() method is called.

The thread is prepared to execute and is awaiting the processor's availability (CPU time) in the runnable environment. This means that the thread has joined the list of threads that are awaiting execution.

Running state

Running denotes that the CPU has given the thread a time slot for execution. A thread from the runnable state joins the running state when it is picked for execution by the thread scheduler.

The thread's run procedure is executed by the processor while it is in the running state. In this stage, the thread carries out its activities directly. A thread can only enter the running state from the runnable state.

Blocked state

When a thread is active, its thread class object exists, but the scheduler cannot choose it for execution. It's no longer active.

Dead state 

A thread immediately exits or enters the dead state after its run() function terminates the execution of sentences. That is, a thread is terminated or destroyed when it leaves the run() process. A thread will stop running when the stop() method is used.

Methods of Thread Class

Several constructors have been declared in the java.lang.Thread class for various uses. Among them are:

  • Thread():  no-argument constructor.
  • Thread (String name): takes a string as an argument.
  • Thread(Runnable r): takes reference (r) of a Runnable object as an argument.
  • Thread(*Runnable r, String r): takes reference (r) of a Runnable object as well as a string as arguments.

Md Ashraf

'KNOWLEDGE WITH ASHRAF' is the platform where you find all the type of knowledge especially on programming based. Our goal is to give you a deeper grasp of technology in specifics that will help you increase your knowledge.

Previous Post Next Post

Contact Form