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.
David Wake
Greenhorn
Joined: Apr 18, 2001
Posts: 17
posted
0
The piggy() method includes a call to start() David
David Wake
Greenhorn
Joined: Apr 18, 2001
Posts: 17
posted
0
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
Marcus Green
arch rival
Rancher
Joined: Sep 14, 1999
Posts: 2813
posted
0
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 =================================================
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).]
Percy Densmore
Ranch Hand
Joined: Mar 06, 2001
Posts: 214
posted
0
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. :-)
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.