PDA

View Full Version : Difference between run() and start() methods in Java Threads


neha
14-01-2010, 04:35 PM
If you want the run() method to be executed in a separate thread, do not invoke it directly; invoke it by calling the start() method.

For Java, the run() method is just another method; so, you can execute it directly, in which case it will be executed in the calling thread and not in its own thread.

public class ThreadDemo {
public static void main(String[] args) {
Counter ct = new Counter();
ct.run();
//ct.start();
System.out.println("The thread has been started");
}
}
class Counter extends Thread {
public void run() {
for ( int i=1; i<=5; i++) {
System.out.println("Count: " + i);
}
}
}

check the output with ct.run() and ct.start(), you will get a better picture.