Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we use run() method to run the thread?  RSS feed

 
neo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,we use the start() method of a thread instance to run the thread. (i know that we don't actually run the thread,but register it with the thread scheduler, and it calls the run method implicitly) but i cud run the thread by directly calling the run method. how is it possible ? and what is the use of start method then? i'm posting the code.i'm sry i cudn't find the ans in FAQs.
Thanks in advance

public class Test1 implements Runnable {
public void run() {
for (int i = 0; i <= 5; i++)
System.out.println("done");
}

public static void main(String args[]) {
Test1 t = new Test1();
Thread s = new Thread(t);
s.run();

}

}
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"run" is a method of any class extending Thread (or implementing Runnable) - nothing distinguishes it from other Java methods, so you can call it directly.

The difference, however, is that calling "run" will NOT start a new thread; it will execute within the currently running thread. Only calling "start" will cause the run method to be executed in a new thread, thus accomplishing the purpose of using a Thread (or Runnable) in the first place.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!