• 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

A question for beginners

 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
Without compiling can you tell the output of the following program

and that of this program.

and this program

What are the results?? why?
Regds.
Rahul.

[This message has been edited by Rahul Mahindrakar (edited August 13, 2000).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code1 : start() is overriden, the run() will not be called; so only "in start" printed
code2: normal way to call thread method, "in run " will be printed
code 3 : super.start() will call Thread.start(), which invoke run(), so both will be the output.
I didn't run it, so pls. tell me if I'm wrong
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wonda,
Lets wait for some more people to respond. But you are right on track.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case 1, since the start method is overridden,the output will be 'in start' and run() will not be executed.
In code 2, the execution takes its normal course,start() evokes run() and the output is ' in run '.
in code 3,the start() is overriden, and inside the method, it calls the superclass's start(),then the run() is invoked... is it true ? what happens here ?
please correct me if I am wrong and tell me what happens in case 3..
regards,
Vidya.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Result of the Code 1 :
" in run"
since start method of Thread class will look for only run method and in main method there is no start() method to invoke public void start() method
(Note: I assume that start is not a key or reserved word of Java, if so there will be a compile time error)
Result of the Code 2 :
" in run"
perfectly alright (is it!)
Result of the Code 3 :
" in run"
Again the same... start() method was not invoked in main method and hence super.start() will also not at all get chance to run the method.
I think Thread class start method is a key to run the run method. Since we are not directly calling the run method (like run())in main method.
Really it is interesting tough code...
Please clarify wherever I am wrong.
Thanks and regards,
[This message has been edited by V Srinivasan (edited August 17, 2000).]
[This message has been edited by V Srinivasan (edited August 17, 2000).]
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most of you are right on this front .
The answers are
1) Prints only "in start " as start has been overriden
2) Prints "in run" as the start method has not been overriden and the default start method of Thread calls run() by default of the current object rather than that of thread due to dynamic polymorphism, creating two theads.
3) Prints both "in start" and "in run" as the start method has been overriden but since it calls the start method of the super class which by default invokes run of the current object rather than that of the thread due to dynamic polymorphism.
for example



The program above prints "out in subclass run" and not "in run of thread1 class" even though run is being called in the start method of the thread1 class.
I posted these questions to show the practical usage of overriding and how this can impact the program output. I also posted these three code questions to show the importance of the start method of the thread class. What does the start method of Thread class do. I enclose what the api says about start()
"Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method)."
The important thing to remember is that the call to start() creates two theads. The same call to run() method would not give the same results and run() would be invoked in the current thread.
Thread t=new Thread(Runnable Object);
t.start(); // creates two new threads
Thread t=new Thread(Runnable Object);
t.run();// does not create two threads
If one were to override the start method without a call to super.start() then neither would there be a call to run() be made nor a new thread created. If there is thus a need to ever override the start method then one should remember to invoke a call to the super.start().
Note : One can invoke the run method of a dead method but one cannot invoke the start method of the dead thread.
Regds.

Rahul P. Mahindrakar

[This message has been edited by Rahul Mahindrakar (edited August 20, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rahul,
Did you say for code 2) Prints both "in start" and "in run" .
But there is no println statement for "in start". OR am I missing
something.

code:
--------------------------------------------------------------------------------
class demo extends Thread{
public void run(){
System.out.println(" in run");
}
public static void main(String args[]){
demo x=new demo();
x.start();
}
}
----------------------------------------------------------------------
Correct me if I am wrong.
Regards
VR
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi VR
Thanks for pointing out the mistake. I have corrected it now. The answer is "in run" only for 2nd code.
Regds.
Rahul P Mahindrakar
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rahul!
Good support keep it up.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Acharya,
Good to see you here on JavaRanch.
Regds.
Rahul P. Mahindrakar
 
vidya
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Rahul....
looking forward to more of this kind..
regards,
Vidya.
 
V Srinivasan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rahul,
Thank you very much. You mentoring is really helps a lot to beginners like me.
Thanks & Regards,
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Mahindrakar:
Hi ,
[b]Without compiling
can you tell the output of the following program

and that of this program.

and this program

What are the results?? why?
Regds.
Rahul.
[This message has been edited by Rahul Mahindrakar (edited August 13, 2000).][/B]



The result from the first snipped code will be:
"in start";
You must expect for start() to call run() and output to be
: in start
in run.
It wont be, because since you override start it won't call run.
So the result is : "in start";
And the last two are going to work "ok".
Second output is : in run
Third: in start
in run
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right on Track AL
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right.I compiled and ran the programs and the results are the same as you computed.

------------------
Java lover from hell!
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the 3rd code snippet is the order of the 2 output statements guaranteed?
thanks,
GP
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"wonda", "vidya", "VR", "al":
Your names are not valid as per the Javaranch name conventions. Please refer to the document located here: http://www.javaranch.com/name.jsp
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Mahindrakar:
The important thing to remember is that the call to start() creates two theads.


I do not understand how start() creates two threads. Two threads are running, the original thread that contains main() and calls start(), and the new thread that is started by start().

Does start() start a thread in the superclass Thread as well as in the subclass (the one that contains run()) ??

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> In the 3rd code snippet is the order of the 2 output
>> statements guaranteed?
>> thanks,
>> GP
No. In all probability, on a single processor machine, "in start" will print before "in run." However, the two statements are being printed by two threads running concurrently. It's possible that the second thread (the one created by super.start()) will print "in run" before the original thread prints "in start"
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long time seeing this post..
any how marilyn murphy,
You are right actually its one additional thread gets created.

------------------

Mahindrakar
IBM Application Server & Developer Forum Moderator

Consultant - Zensar Technologies ,Pune India.
SCJP2, SCJD2 & SCJEA (Part I)
 
Rahul Mahindrakar
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This output should help give you total number of threads

------------------

Mahindrakar
IBM Application Server & Developer Forum Moderator

Consultant - Zensar Technologies ,Pune India.
SCJP2, SCJD2 & SCJEA (Part I)
 
marilyn murphy
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the clarification, Rahul
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code1 : start() is overriden, the run() will not be called; so only "in start" printed
code2: normal way to call thread method, "in run " will be printed
code 3 : super.start() will call Thread.start(), which invoke run(), so after starting thread control will come back to the next statemtn and will print "in start" and then when that thread occupy its time and will print in run.
I didn't run it, so pls. tell me if I'm wrong

reply
    Bookmark Topic Watch Topic
  • New Topic