This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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 ]
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 ]
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.
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
posted
0
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
posted
0
thanks guys very much! Krussi
krussi rong
Ranch Hand
Joined: Jan 30, 2002
Posts: 62
posted
0
thanks guys very much! Krussi
Younes Essouabni
Ranch Hand
Joined: Jan 13, 2002
Posts: 479
posted
0
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.