GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green #3 Q19 (Thread question) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green #3 Q19 (Thread question)" Watch "Marcus Green #3 Q19 (Thread question)" New topic
Author

Marcus Green #3 Q19 (Thread question)

Larry Lecomte
Ranch Hand

Joined: Jun 14, 2002
Posts: 37
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"
Why is the answer POSSIBLE output of "vandeleur 0 1 2 3" ? I was sure that this was ALWAYS in the
output, because the thread is NOT a daemon thread...
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Larry
The main Thread and the Thread tux both have the same priority and becasue thread scheduling is OS dependent either one could preempt the other at any time. Or the main Thread could finish running before the tux Thread modifys the String. Because the String starts with the value of vandeleur, vandeleur will alwasy be printed. IF the tux Threads' runs before the main Thread prints the String then the String may not be just vandeleur.
Hope that helps


Dave
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Please see this thread.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Larry, I understand that this question can seem confusing but it covers an important point. No matter how many times you run this code yourself and get the expected result it does not mean that the result you see is guaranteed. The thread related topics on the exam are quite narrow, but you need to understand them well.


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Marcus, would you agree that the following are also possible, though unlikely, outputs from your program?
"vandeleur 0"
"vandeleur 0 1"
"vandeleur 0 1 2"


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Ron:
I would agree with you. Can we define a string object as syncronized to avoid such output?
[ September 23, 2002: Message edited by: Barkat Mardhani ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
String is a final class. You can't define a subclass with synchronized methods. Also, String objects are immutable so there is no point to synchronizing access to them.
What you probably want to do instead is synchronize access to the String variable Tux.sName. Since this is a static variable, you'll need either static synchronized methods or blocks that start with "synchronized (Tux.class)" .
[ September 23, 2002: Message edited by: Ron Newman ]
[ September 23, 2002: Message edited by: Ron Newman ]
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Ron, yes I agree those are possible alternative outputs.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Ron:
q1: String objects are immutable. Are String members also immutable. If yes, why following is allowed:
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
This statement creates a new String object whose contents are the two argument strings appended together.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
but sName is class member as well as a string type. So if string are not mutable, class members can not be either...Right?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hi Barkat,
sName is a reference to a String object.
After sName = sName + "wiggy"; it's the reference
that has changed. It's now pointing to a new String object, as Ron said.
BTW, if sName was final, the new String object could not be assigned to the sName variable.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
hi,
the sName inside the method refers to the method's argument. The class member sName has been shadowed and it was not affected by the assignment (sName = sName + "wiggy");


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Shishio San said: the sName inside the method refers to the method's argument. The class member sName has been shadowed and it was not affected by the assignment (sName = sName + "wiggy");

Exactly Shishio San! The sName inside the method is pointing to the new string, but the member sName is still pointing to the old one.
 
GeeCON Prague 2014
 
subject: Marcus Green #3 Q19 (Thread question)