Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
krussi rong
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why does it print "Vandeleur" instead of "Vandeleur 0 1 2 3"?
Francisco
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys very much!
Krussi
 
krussi rong
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys very much!
Krussi
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well seen Jessica, I was scratching my head
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic