Member-only story
Difference Between start() and run() in Java
🔒 This is a Medium member-only article. If you’re not a Medium member, you can read the full article for free on my blog: Difference Between start() and run() in Java.
In Java multithreading, two commonly discussed methods are start()
and run()
. At first glance, they may look similar—after all, both are related to starting a thread—but they function in very different ways.
If you’re learning Java or preparing for interviews, it’s crucial to understand the key differences between start()
and run()
, how they work, when to use them, and what happens behind the scenes.
In this article, we’ll explore these two methods using clear explanations, practical code examples, and a comparison table.
Introduction to Multithreading in Java
Java supports multithreading, allowing concurrent execution of two or more parts of a program for maximum utilization of CPU. To implement multithreading, Java provides the Thread
class and the Runnable
interface.
start()
andrun()
are central to this threading mechanism.- But confusing them can lead to incorrect program behavior.
What is start()
in Java?
The start()
method is a member of the java.lang.Thread
class. It is used to create a new thread of execution and begin the execution of the code written in the run()
method.
When you call start()
on a thread object:
- A new thread is created by the JVM.
- That thread executes the
run()
method asynchronously.
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Running in: " + Thread.currentThread().getName());
}
}
public class StartDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start(); // creates a new thread
}
}
Output:
Running in: Thread-0
What is run()
in Java?
The run()
method is defined in the Runnable
interface. It contains the actual code that you want the thread to execute.
However, if you call run()
directly like a regular method, no new thread is created—the code executes in the current thread.