The Wayback Machine - https://web.archive.org/web/20240728054044/https://www.geeksforgeeks.org/difference-between-thread-start-and-thread-run-in-java/
Open In App

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

Last Updated : 21 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

In Java’s multi-threading concept, start() and run() are the two most important methods. Below are some of the differences between the Thread.start() and Thread.run() methods:

  1. New Thread creation: When a program calls the start() method, a new thread is created and then the run() method is executed. But if we directly call the run() method then no new thread will be created and run() method will be executed as a normal method call on the current calling thread itself and no multi-threading will take place. Let us understand it with an example: 

Java




class MyThread extends Thread {
    public void run()
    {
        System.out.println("Current thread name: "
                           + Thread.currentThread().getName());
        System.out.println("run() method called");
    }
}
 
class GeeksforGeeks {
    public static void main(String[] args)
    {
        MyThread t = new MyThread();
        t.start();
    }
}


Output:

Current thread name: Thread-0
run() method called
  1. As we can see in the above example, when we call the start() method of our thread class instance, a new thread is created with default name Thread-0 and then run() method is called and everything inside it is executed on the newly created thread. Now, let us try to call run() method directly instead of start() method: 

Java




class MyThread extends Thread {
    public void run()
    {
        System.out.println("Current thread name: "
                           + Thread.currentThread().getName());
 
        System.out.println("run() method called");
    }
}
 
class GeeksforGeeks {
    public static void main(String[] args)
    {
        MyThread t = new MyThread();
        t.run();
    }
}


Output:

Current thread name: main
run() method called
  1. As we can see in the above example, when we called the run() method of our MyThread class, no new thread is created and the run() method is executed on the current thread i.e. main thread. Hence, no multi-threading took place. The run() method is called as a normal function call.
  2. Multiple invocation: In Java’s multi-threading concept, another most important difference between start() and run() method is that we can’t call the start() method twice otherwise it will throw an IllegalStateException whereas run() method can be called multiple times as it is just a normal method calling. Let us understand it with an example: 

Java




class MyThread extends Thread {
    public void run()
    {
        System.out.println("Current thread name: "
                           + Thread.currentThread().getName());
 
        System.out.println("run() method called");
    }
}
 
class GeeksforGeeks {
    public static void main(String[] args)
    {
        MyThread t = new MyThread();
        t.start();
        t.start();
    }
}


  1. Output:
Current thread name: Thread-0
run() method called
Exception in thread "main" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:708)
    at GeeksforGeeks.main(File.java:11)
  1. As we can see in the above example, calling start() method again raises java.lang.IllegalThreadStateException. Now, let us try to call run() method twice: 

Java




class MyThread extends Thread {
    public void run()
    {
        System.out.println("Current thread name: "
                           + Thread.currentThread().getName());
        System.out.println("run() method called");
    }
}
 
class GeeksforGeeks {
    public static void main(String[] args)
    {
        MyThread t = new MyThread();
        t.run();
        t.run();
    }
}


Output:

Current thread name: main
run() method called
Current thread name: main
run() method called
  1. As we can see in the above example, calling run() method twice doesn’t raise any exception and it is executed twice as expected but on the main thread itself.Summary
start() run()
Creates a new thread and the run() method is executed on the newly created thread. No new thread is created and the run() method is executed on the calling thread itself.
Can’t be invoked more than one time otherwise throws java.lang.IllegalStateException Multiple invocation is possible
Defined in java.lang.Thread class. Defined in java.lang.Runnable interface and must be overridden in the implementing class.


Previous Article
Next Article

Similar Reads

Why Thread.stop(), Thread.suspend(), and Thread.resume() Methods are Deprecated After JDK 1.1 Version?
The Thread class contains constructors and methods for creating and operating on threads. Thread is a subclass of Object that implements the Runnable interface. There are many methods in Thread Class but some of them are deprecated as of JDK 1.1. In this article, we will understand the reason behind it. Deprecated methods are those that are no long
5 min read
Difference Between Thread ID and Thread Handle
Prerequisite: Thread in Operating System Thread Id is a long positive integer that is created when the thread was created. During the entire lifecycle of a thread, the thread ID is unique and remains unchanged. It can be reused when the thread is terminated. The Thread get_id() method can be studied here where we can see how it's used. Thread Handl
3 min read
Difference between User Level thread and Kernel Level thread
User-level threads are threads that are managed entirely by the user-level thread library, without any direct intervention from the operating system's kernel, whereas, Kernel-level threads are threads that are managed directly by the operating system's kernel. In this article, we will see the overview of the User Level thread and Kernel Level threa
4 min read
Naming a thread and fetching name of current thread in Java
Thread can be referred to as a lightweight process. Thread uses fewer resources to create and exist in the process; thread shares process resources. The main thread of Java is the thread that is started when the program starts. now let us discuss the eccentric concept of with what ways we can name a thread. Methods: There are two ways by which we c
5 min read
run() Method in Java Thread
The run() method is available in the thread class constructed using a separate Runnable object. Otherwise, this method does nothing and returns. We can call the run() method multiple times. The run() method can be called in two ways which are as follows: Using the start() method.Using the run() method itself. Syntax: public void run() { //statement
3 min read
Overloading of Thread class run() method
Overloading of run() method is possible. But Thread class start() method can invoke no-argument method. The other overloaded method we have to call explicitly like a normal method call. // Java Program to illustrate the behavior of // run() method overloading class Geeks extends Thread { public void run() { System.out.println("GeeksforGeeks
2 min read
Overriding of Thread class start() method
Whenever we override start() method then our start() method will be executed just like a normal method call and new thread wont be created. We can override start/run method of Thread class because it is not final. But it is not recommended to override start() method, otherwise it ruins multi-threading concept. // Java program illustrate the concept
2 min read
Difference between Compile-time and Run-time Polymorphism in Java
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. In this article, we will see the difference between two types of polymorphisms, compile time and run time. Compile Time Polymorphism: Whenever an object is bound with its functionality at the
3 min read
Difference between Process and Kernel Thread
1. Process: Process is an activity of executing a program. Process is of two types - User process and System process. Process control block controls the operation of the process. 2. Kernel Thread: Kernel thread is a type of thread in which threads of a process are managed at kernel level. Kernel threads are scheduled by operating system (kernel mod
1 min read
Difference between Process and User Level Thread
1. Process : Process is an activity of executing a program. Process is of two types - User process and System process. Process control block controls the operation of the process. 2. User Level Thread : On the basis of level, threads are of two types: User level Kernel Level User level thread is a type of thread in which threads of a process are ma
2 min read
three90RightbarBannerImg