×

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods.

Thread.start():

The thread's execution is initiated by invoking the start() method. The start() method operates two threads they are: current thread and other threads.

The run() is executed by the start method or by calling itself.

The tasks performed by the start thread are:

  • Starting of a New Thread.
  • Moving the thread from new to runnable state.
  • The thread's target run() method will be called once it has a chance to execute.

Example Program:

//program for implementing the start method using Thread class
class TStart extends Thread
{
// method for running the thread
public void run()
{
System.out.println(“Running State of Thread”);
}
}
class  Ex 
{
// main method
public static void main(String args[])
{
TStart t1 = new TStart();
// using the start method on thread
t1.start();
}
}

Output:

Running state of Thread

Example 2:

// program for start() method using Runnable Interface
class Tstart implements Runnable
{
public void run()
{
System.out.println(“Running of Thread”);
}
public static void main(String args[])
{
Tstart r1 = new Tstart();
Thread t1 = new Thread(r1);
t1.start();
}
}

Output:

Running of Thread

Thread.run():

If a thread was created using a separate Runnable object, the thread class's run() method will be invoked; otherwise, it will do nothing and return. The code supplied in the run() method is performed when the run() function is called. The run() method can be executed repeatedly.

The run() method can be invoked by start() method or by itself.

Program:

//program for implementing the run by start method
class TStart implements Runnable
{
// method for running the thread
public void run()
{
System.out.println(“Running State of Thread”);
}
}
class  Ex 
{
// main method
public static void main(String args[])
{
TStart r = new TStart();
Thread t1 = new Thread(r);
// using the start method on thread for invoking run method
t1.start();
}
}

Output:

Running state of Thread

Example 2:

// program to implement the run() method directly.
import java.io. *;
import java. util. *;
public class Main extends Thread  
{    
    public void run()  
    {    
        System.out.println("Running State of Thread");    
    }    
    public static void main(String args[])  
    {    
        Main t1=new Main();    
        // directly using the run() method
        t1.run();   
    }    
}   

Output:

Running State of Thread

Difference between Thread.start() and Thread.run().

The difference between Thread.start() and Thread.run() are:

Start()Run()
The start method is defined in thread class.The run method is defined in runnable interface.
  The creation of thread and running run method operations are done by using the start method  Only the execution of the run method and thread is cant be created here.
The start method can only be invoked once throoughout the program  The run method can able to executed many times.
  • Creation of new Thread:

A new thread is generated when a programme invokes the start() method, and the run() method is then called. However, if we explicitly call the run() function, no new thread will be formed; instead, the run() method will be called as a regular procedure on the thread that is now calling; multi-threading will not occur.

Example Program:

//program for implementing the start method using Thread class
class TStart extends Thread
{
// method for running the thread
public void run()
{
System.out.println(“Current Thread:”+Thread.currentThread().getName());
System.out.println(“Run() method called”);


}
}
class  Ex 
{
// main method
public static void main(String args[])
{
TStart t1 = new TStart();
// using the start method on thread
t1.start();
}
}

Output:

Current Thread name: Thread-0
Run() method called.

In the above program, by calling the start method we are able to create a thread. Whereas when by calling the run() we cannot able to create a thread. The program to run the run() method directly is:

// program for run method
class TStart extends Thread
{
// method for running the thread
public void run()
{
System.out.println(“Current Thread:”+Thread.currentThread().getName());
System.out.println(“Run() method called”);


}
}
class  Ex 
{
// main method
public static void main(String args[])
{
TStart t1 = new TStart();
t1.run();
}
}

Output:

Current Thread: Main
Successfully executed Run() method
  • Multiple Execution:

The start() method can only be invoked once in the program, but the run() method can able to invoked many times.

The example for invoking the start() method multiple times.

Example Program:

import java.io.*;
//program for implementing the start method using Thread class
class TStart extends Thread
{
// method for running the thread
public void run()
{
System.out.println(“Current Thread:”+Thread.currentThread().getName());
System.out.println(“Run() method called”);


}
// main method
public static void main(String args[])
{
TStart t1 = new TStart();
// using the start method on thread
t1.start();
t1.start();
}
}

Output:

Exception in thread "main" java.lang.IllegalThreadStateException
        at java.base/java.lang.Thread.start(Thread.java:794)
        at Main.main(Main.java:18)


Current Thread:Thread-0
Successfully executed run method

In the above program, the invoking of the start() method many times, it raises an exception.

The Program for executing the run() method multiple times.

Example:

// program to implement the run() method directly twice.
import java.io. *;
import java. util. *;
public class Main extends Thread  
{    
    public void run()  
    {    
        System.out.println(“Current Thread:”+Thread.currentThread().getName());
System.out.println(“Run() method called”);


    }    
    public static void main(String args[])  
    {    
        Main t1=new Main();    
        // directly using the run() method
        t1.run();   
t1.run();
    }    
}   

Output:

Current Thread: Main
Successfully executed Run() method
Current Thread: Main
Successfully executed Run() method

In the above program, the run method is executed twice and it doesn’t show an exception.

Summary:

The start and run are the methods which are used in the Thread concepts. The main differences between start and run methods in the thread are “creation of the thread” and “Multiple Execution”.

The start method can able to create a thread. It is the initial step for the creation of the thread. The start method can able to invoke the run method.

The run() method cannot able to create a thread. The run() method is defined in the Runnable interface. The run() method can be executed many times.


Related Topics

Java String Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

How to Send SMS in Java with Example

Sending SMS messages in Java is a fairly common task, and there are a number of libraries and APIs available to help you do it. One popular option is to...

2 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

How to calculate time difference in Java

In this tutorial, we learned about how to calculate time differences in java language and which methods use to find the difference and its example. Prerequisite To understand this example, you need...

5 minutes read.

Ordinal Number in Java

What is the Ordinal Number in Java? An object's or person's position or rank can be expressed mathematically using an ordinal number. Depending on the criteria used to establish the positions,...

3 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Automatic Resource Management

In computer programming, resource management refers to the wide set of techniques for the effective management of the system resources. There are two ways of management of resources: The computer program itself...

2 minutes read.