Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Quetion

 
Venkatachalam Palanisamy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
class Thre extends Thread
{
public void run()
{
System.out.println("Running........");
}
public void run(int j)
{
System.out.println("Yes----> "+j);
}
public static void main(String[] args)
{
Thre t = new Thre();
t.start();
t.run(200);
}
}
why the output of the above code snippet is
Yes -----> 200
Running........
i expected the output in the reverse order..
can any one give me the explanation or relavent URL.
Thanks & Regards,
Venkatachalam VP
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call run(int), it executes immediately. When you call start(), you have scheduled the run() method for asynchronous execution -- it could very well finish executing before the call to run(int), but because of all the overhead of switching threads and whatnot it probably won't.
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I just transferred the program from "extends Thread" to "implements Runnable".
class Thre1 implements Runnable {
public void run() {
System.out.println("Running........");
}
public void run(int j) {
System.out.println("Yes----> "+j);
}
public static void main(String[] args) {
Thre1 thre1 = new Thre1();
Thread t = new Thread(thre1);
t.start();
// t.run(200) is not possible because t is Thread
// object and Thread class has only run() method
// and not run(int j) method!!
thre1.run(200);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic