wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
Hi,
here is the question:

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 "vandeleur" but possible output of "vandeleur 0 1 2 3"

the anwser is 4)
My question is when runs the line 1, so the
start() in method piggy will run as start a
thread to call the run() method?
and when the probably output of "vandeleur"
when the possible output of "vandeleur 0 1 2 3"?
thanks
Krussi
[ Added UBB [CODE] tags to add whitespace, improve readibility ]
[ June 05, 2002: Message edited by: Jessica Sant ]
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

I think you're asking why the answer is #4?
Compilation and probably output of "vandeleur" but possible output of "vandeleur 0 1 2 3"
Lets step through the code:
1st you create an instance of the class Tux,
2nd call .piggy(String) -- which starts the thread.
3rd call System.out.println(String). "vandeleur"
**BUT: between step 2 and step 3 there's a chance that this new thread that you started MIGHT actually run -- so its possible that it could have modified sName before you print it in step 3 (thus printing "vandeleur 0 1 2 3" instead).
Also, the reason its not printing "vandeleur wiggy 0 1 2 3" is because the method .piggy(String) makes modifications to the local variable sName, not the static class variable sName.
Does that help? Is that what you were asking?
[ June 05, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
but why does it print "Vandeleur" instead of "Vandeleur 0 1 2 3"?
Francisco


Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Francisco A Guimaraes:
but why does it print "Vandeleur" instead of "Vandeleur 0 1 2 3"?

I modified my original post a tad... does that clear things up?
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182
yes, thanks Jessica. As a matter of fact, after I posted the message, I put a "join()" after the "start()", and it always printed "vandeleur 0 1 2 3". I think that means that when there wasn�t the "join()", the thread didn�t had time to modify sName, right?
Francisco
krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
thanks guys very much!
Krussi
krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
thanks guys very much!
Krussi
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Well seen Jessica, I was scratching my head


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
 
jQuery in Action, 2nd edition
 
subject: Thread