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

In Java, threads are an essential part of concurrent programming, and the Thread class provides two methods to create and start a new thread: run() and start(). While both methods are used to execute a thread, there is a significant difference between the two.

run() Method

The run() method is a simple method defined in the Thread class. It contains the code that is executed when a thread is started. The run() method can be overridden by a subclass of Thread or by an object of a class that implements the Runnable interface. The run() method is a non-static method, so it cannot be called directly by the class name.

The run() method is executed in the current thread, not in a new thread. When the run() method is called, it runs in the same thread in which it was called. The run() method does not create a new thread; it simply executes the code in the current thread.

start() Method

The start() method is a crucial method in the Thread class. When called, it creates a new thread, and the code in the run() method is executed in that new thread. The start() method is a non-blocking method; it returns immediately, and the new thread starts executing the code in the run() method.

Once a thread is started with the start() method, it is considered a new thread of execution. The new thread runs independently of the thread that created it. The start() method creates a new thread in which the code in the run() method is executed.

Differences between run() and start()

The primary difference between the run() and start() methods is that the run() method executes the code in the current thread, while the start() method creates a new thread and executes the code in that new thread.

Another key difference is that the start() method is non-blocking, while the run() method is blocking. When the start() method is called, it returns immediately, and the new thread starts executing the code in the run() method. In contrast, the run() method runs in the same thread in which it was called, and it does not return until the code in the method is completed.

It is essential to note that calling the run() method does not create a new thread, and it does not execute the code in a separate thread. If the run() method is called directly, it will execute in the current thread and will not be concurrent with any other threads.

Conclusion

In conclusion, the run() method is used to execute the code in the current thread, while the start() method is used to create a new thread and execute the code in that new thread. The start() method is non-blocking, while the run() method is blocking. When creating a new thread, it is essential to use the start() method to ensure that the code is executed concurrently with other threads.