Junior — Middle
What is the difference between calling the run method and using the start method when working with the Runnable interface?
sobes.tech AI
Answer from AI
The run() method of the Runnable interface contains code that should be executed in a separate thread, but calling run() directly is just a normal method call in the current thread, without creating a new thread.
The start() method of the Thread class creates a new thread and calls the run() method inside it. Therefore, to run code in a new thread, you need to create a Thread object, pass it a Runnable, and call start(), not run() directly.
Example:
Runnable task = () -> System.out.println("Executing in thread " + Thread.currentThread().getName());
// Calling run() directly — executes in the current thread
task.run();
// Running in a new thread
Thread thread = new Thread(task);
thread.start();