Can we call run() method without start() method in thread?
Yes, you can call the run()
method of a Thread
or Runnable
object directly, but doing so will not create a new thread of execution. Instead, it will simply execute the run()
method in the current thread, just like calling any other method.
Understanding run()
vs start()
run()
Method
The run()
method contains the code that constitutes the new thread's task. When you call the run()
method directly, it does not create a new thread; it simply executes the method in the current thread.
public class MyRunnable implements Runnable { @Override public void run() { System.out.println("Running in: " + Thread.currentThread().getName()); }
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
myRunnable.run(); // This runs in the main thread
}
}
Output:
Running in: main
start()
Method
The start()
method creates a new thread and then calls the run()
method in that new thread. This is the correct way to start a new thread of execution.
public class MyRunnable implements Runnable { @Override public void run() { System.out.println("Running in: " + Thread.currentThread().getName()); }
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start(); // This starts a new thread
}
}
Output:
Running in: Thread-0
Key Differences
- Thread Creation:
run()
: No new thread is created; the method runs in the current thread.start()
: A new thread is created, andrun()
is executed in that new thread.- Concurrency:
run()
: No concurrent execution.start()
: Allows concurrent execution.
Why It Matters
- Using
run()
directly: This is essentially just a normal method call. There is no parallelism; the code inrun()
executes sequentially within the current thread. - Using
start()
: This leverages the Java concurrency framework to run tasks in parallel, allowing multiple threads to execute simultaneously.
Conclusion
To achieve actual multithreading and run tasks in parallel, you must call start()
on a Thread
object, which internally calls the run()
method in a new thread of execution. Calling run()
directly is useful only for testing or when you do not need concurrent execution.