My Hindi Forum

Go Back   My Hindi Forum > Miscellaneous > Tech Talks
Home Rules Facebook Register FAQ Community

Reply
 
Thread Tools Display Modes
Old 14-01-2010, 05:35 PM   #1
neha
Member
 
neha's Avatar
 
Join Date: Dec 2009
Posts: 152
Rep Power: 15
neha will become famous soon enough
Default 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);
    }
 }
}
check the output with ct.run() and ct.start(), you will get a better picture.
neha is offline   Reply With Quote
Reply

Bookmarks

Tags
threads


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT +5. The time now is 09:03 AM.


Powered by: vBulletin
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
MyHindiForum.com is not responsible for the views and opinion of the posters. The posters and only posters shall be liable for any copyright infringement.