JAVA-TRICK-3: What Happens If You Call the Run Method Directly Instead of Start()?
Question:
What is the difference between calling run() and start() for a thread?
Explanation:
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
public class Test {
public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.run(); // Executes in main thread
t1.start(); // Executes in a new thread
}
}
Output:
Thread running
Thread running