• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

super.start()

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi! can u pl. explain the sequence of the statements as output in the code given below?
thanx.
ashok.
___________
class fort extends Thread {
public void start(){
super.start();
System.out.println("in start");
}
public void run() {
System.out.println("in run");
}
public static void main(String args[]){
fort t = new fort();
t.start();
}
}
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output will be
in start
in run
According to the api doc the start method
"Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread."
So my understanding is even if you call super.start() , this.Start() is executed followed by this.run()..
Anybody correct me if I am wrong.

Originally posted by ashok khetan:
hi! can u pl. explain the sequence of the statements as output in the code given below?
thanx.
ashok.
___________
class fort extends Thread {
public void start(){
super.start();
System.out.println("in start");
}
public void run() {
System.out.println("in run");
}
public static void main(String args[]){
fort t = new fort();
t.start();
}
}


 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The moment you override the start() method, the ability of this method to call the run() method is GONE!!
So to make the start() initiate the run() we use super.start()
t.start() in this case, is just a normal instance member invocation and the super.start() inside does what its supposed to do
HTH
Ragu
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why is "in start" printed before "in run"? Shouldn't super.start() print "in run" when it invoke run(), then "in start" was printed?
Thanks.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No the output is correct, but I think the reason why it is like that is that the start method returns immediately.
Val
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check this modified code:
class fort extends Thread {
public void start(){
super.start();
for (int i=0; i<100;i++) System.out.println("in start");
}
public void run() {
for (int i=0; i<100;i++) System.out.println("in run");
}
public static void main(String args[]){
fort t = new fort();
t.start();
}
}
so I think it's just because the println("in start"); get the cpu slightly earlier.

Originally posted by Cameron Park:
I don't understand why is "in start" printed before "in run"? Shouldn't super.start() print "in run" when it invoke run(), then "in start" was printed?
Thanks.


 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valentin,
I idea is not yet cleared, if you try to compile the code provided by Roger, it will sometimes print in start first ans sometimes print it later why it is so.
Please give solid concept behind this.
Nisheeth.
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nisheeth Kaushal:
Hi Valentin,
I idea is not yet cleared, if you try to compile the code provided by Roger, it will sometimes print in start first ans sometimes print it later why it is so.
Please give solid concept behind this.
Nisheeth.


when u call start() method of Thread , then it just registers the tread with thread schedular, now it is thread schedular's responsibility when it gives cpu to run the thread.
I think sequence of execution totally depend on thread schedular.
CMIW
------------------
Regards
Ravish
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ravish kumar:
when u call start() method of Thread , then it just registers the tread with thread schedular, now it is thread schedular's responsibility when it gives cpu to run the thread.
I think sequence of execution totally depend on thread schedular.


but when i call the super.start(), why does it causes executing this.run()? does the super.start() register the thread on thread schedular with this reference? so the thread schedular executes the the subclass's run() but not the super's ?
 
R K Singh
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Yin Ming:
but when i call the super.start(), why does it causes executing this.run()? does the super.start() register the thread on thread schedular with this reference? so the thread schedular executes the the subclass's run() but not the super's ?
[/B]


I think in start() there should be some thing like..
Thread threadObj = ourObject
as our ourObject is subclass of Thread so when threadObj's run is scheduled then our run is executed coz of overriding
I am downloading source of java2sdk ,will get in touch soon..
CMIW

------------------
Regards
Ravish
 
reply
    Bookmark Topic Watch Topic
  • New Topic