• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling Directly run() method

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class demo extends Thread {
public void run(){
for(int i=0;i<10;i++)
System.out.println(i);
}
}
class test{
public static void main(string arg[]){
demo dt = new demo();
dt.run();
System.out.println("main thread output");
}
}
select any 1

A) Code will give compilation error on line 10, since run can not be called directly.
B) Code will compile without error, but will not show any output.
C) Code will compile without errors, but will show output unsing current Thread only
D) Code will give compilation error on line 9.
====
Answer is C
Why???, any description pls.
I selected option A, if C is correct then why we call start() method, why dont directly run() method. is this call like any other method call, and it wont register it in Thread-sceduler?
-FAF
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
ur guess was right.it would've worked if there is call to start method of thread. but unfortunely there is no any such method.
hence, when u call run() meth w.r.t.the object, it treats that method as a ordinary method n excecutes it.
that's all.
lalit
 
Faisal Farooqui
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you call run, that is an ordinary method call and it will execute it like it. If you call the start method (you inherit it from the Thread class), your run method will behave like a Thread.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic