• 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

Marcus Green exam #1 question 19

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question states:
What will happend when you attempt to compile and run the following code?
public class Tux extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);

}
public void piggy(String sName){
sName = sName + " wiggy";
start();
}
public void run(){

for(int i=0;i < 4; i++){
sName = sName + " " + i;

}
}
}
1) Compile time error
2) Compilation and output of "vandeleur wiggy"
3) Compilation and output of "vandeleur wiggy 0 1 2 3"
4) Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3"
The answer says:
4) Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3"
If that seems a vauge answer it is because you cannot be certain of the system that the underlying OS uses for allocating cycles for a Thread. The chances are that once the thread has been spun off in the call to start in the method piggy the main method will run to completion and the value of sName will still be vandeluer before the Thread modifies it. You cannot be certain of this though.
----------------
How can this be? The thread was never started so the run method will not be called. 'piggy' will be called because it is directly called in the main method. I thought the answer would be 2. Can someone explain why I'm wrong? Thanks.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The piggy() method includes a call to start()
David
 
David Wake
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I missed your other questions. The reason "wiggy" does not get printed is that sName is passed by value to the piggy() method. When the line
sName = sName + " wiggy" ;
is executed, only the copy local to the piggy() method is altered.
The reason why we cannot be sure what the output will be is that things depend on the implementation of the threading mechanism. once the line
start()
is executed in the piggy() method, we have 2 threads: the main thread and the new one just started. There is now a race between the two threads: how much will the new thread do before the main thread executes
System.out.println(sName) ;
??
If it does nothing, then "vandeleur" alone will be printed. However, if it does manage to do something, then numbers may be appended to the output. Indeed, I believe that any of "vandeleur 0", "vandeleur 0 1", "vandeleur 0 1 2", or "vandeleur 0 1 2 3" are possible.
If the underlying threading model is pre-emptive then "vandeleur 0 1 2 3" will be printed. If the underlying threading model is time-sliced then in all probability "vandeleur" will be printed, but anything is possible.
Hope that helped.
David
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent explanation David. Note that this question was previously rather flawed and was updated and corrected as a result of a post at Javaranch. Isn't the web (and the ranch) wonderful.
Marcus
------------------
http://www.jchq.net Mock Exams, FAQ, Tutorial, Links, Book reviews
=================================================
Almost as good as JavaRanch
=================================================
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
From which mockexam u got this Question? I just want to go thru...
Madhuri.
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Sorry. I did not notice it is clearly stated from marcus green #1.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
piggy? wiggy?
Can you tell Marcus is from England?
[This message has been edited by Thomas Paul (edited April 26, 2001).]
 
Marcus Green
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would that be hoggy woggy in the US version of English?
See flag at http://www.jchg.net
Ugh the g from pig got into my url and I had it as www.jchg <===
Marcus
------------------
http://www.jchq.net Mock Exams, FAQ, Tutorial, Links, Book reviews
=================================================
Almost as good as JavaRanch
=================================================
[This message has been edited by Marcus Green (edited May 04, 2001).]
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus,
Your 'See flag line...' has a typo in the URL.
Regards,
Percy
PS I accidentally sent you an email to theWellsCafe, just disregard it. :-)
reply
    Bookmark Topic Watch Topic
  • New Topic