×

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 Pi

What is Pi? There are many formulas in geometry that employ the Pi constant to calculate things like circumference, area, and volume. A circle's circumference divided by its diameter yields a...

3 minutes read.

Median Of Stream Of Running Integers in Java

An integer array is given to us. Compute the median of the elements traversed so far in the input array. For the sake of simplicity, assume that there are no...

10 minutes read.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

Java Numbers

We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc....

2 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

How to Convert Hexadecimal to Decimal in Java

How to Convert Hexadecimal to Decimal in Java There are two methods to convert Hexadecimal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method It is a static method of...

2 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

Constructor in Java with Example

Java Constructor  The constructor is used for object initialization. It's a block of code that initializes a newly created object. It contains a collection of statements that are executed at the...

5 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

Java Program to Create Set of Pairs Using HashSet

HashSet in Java: HashSet is an assortment in Java that has a place with java.util bundle. It inside utilises HashMap to store components. It is an execution of the Set connection...

11 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.