|
14-01-2010, 05:35 PM | #1 |
Member
Join Date: Dec 2009
Posts: 152
Rep Power: 15 |
Difference between run() and start() methods in Java Threads
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. Code:
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); } } } |
Bookmarks |
Tags |
threads |
|
|