Difference between start() and run()

Difference between start() and run()

The Thread class provides two key methods for creating and running threads: start() and run(). Understanding the difference between these two methods is crucial for correctly implementing multithreading in your applications.

start() and run()

start() Method

  • Purpose: The start() method is used to begin the execution of a new thread. When you call start(), the Java Virtual Machine (JVM) calls the run() method of the Thread object in a new thread.
  • Functionality: This method does not call the run() method directly. Instead, it schedules the thread for execution, allowing the JVM to allocate the necessary resources and manage the thread independently.
  • Thread Creation: Calling start() creates a new thread and invokes the run() method in that new thread.

run() Method

  • Purpose: The run() method defines the code that constitutes the new thread’s task. It can be called directly, but doing so will not start a new thread; instead, it will execute the run() method in the current thread.
  • Functionality: If called directly, the run() method runs in the same thread that calls it, without creating a new thread.
  • Thread Creation: Calling run() directly does not create a new thread; it simply executes the method in the context of the existing thread.

Using start() Method
Example

java
class MyThread extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Thread ID: " + Thread.currentThread().getId() + " Value: " + i);
            try {
                Thread.sleep(500); // Sleep for 500 milliseconds
            } catch (InterruptedException e) {
                System.out.println(e);
            }
        }
    }
}

public class StartMethodExample {
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
        t1.start(); // This will create a new thread and call the run() method in that new thread
    }
}


  Using run() Method Directly

java
class MyThread extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Thread ID: " + Thread.currentThread().getId() + " Value: " + i);
            try {
                Thread.sleep(500); // Sleep for 500 milliseconds
            } catch (InterruptedException e) {
                System.out.println(e);
            }
        }
    }
}

public class RunMethodExample {
    public static void main(String[] args) {
        MyThread t1 = new MyThread();
        t1.run(); // This will NOT create a new thread; it will run the run() method in the current thread
    }
}

Explanation start() and run()

  • Using start() Method:
  • When t1.start() is called, it initiates a new thread and the run() method is executed within this new thread. This allows for concurrent execution.
  • Using run() Method Directly:
  • When t1.run() is called, it does not create a new thread. Instead, the run() method is executed in the context of the current thread, behaving like a regular method call.

Homepage

Readmore