Hi all, Can anybody explain how the following program works? Thanks. 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 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" #4 is correct
Alex Sbityakov
Ranch Hand
Joined: Jul 23, 2001
Posts: 49
posted
0
Here's the rundown of what happens: 1. Tux is initialized inside main 2. method piggy is called 3. piggy modifies the parameter sname (not class member) 4. piggy calls start(), which begins the execution of run() Now we have a race condition. Basically we don't know which will execute first: the print statement or the loop inside run(). That's why the answer is 4
Samira Mastali
Ranch Hand
Joined: Aug 03, 2001
Posts: 74
posted
0
Thanks Alex for your nice answer. Samira
Muhammad Farooq
Ranch Hand
Joined: May 08, 2001
Posts: 356
posted
0
So the output can also be: "0 1 2 3" or "0 1 2 3 vandelur" or "vandelur" or "vandeleur 0 1 2 3" Any comments, --Farooq
Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
swati bannore
Ranch Hand
Joined: Oct 18, 2000
Posts: 201
posted
0
Hi all, I am always getting the output of "vandeleur". This means "run()" method is not executed at all,isn't it? But WHY?? Can Someone help clear my confusion... Thanx
Swati Kale
SCJP
SCWCD
swati bannore
Ranch Hand
Joined: Oct 18, 2000
Posts: 201
posted
0
I think I got the code.!! But since we r not printing from "run()" method, it will never print 0,1,2 etc. The println statement is after a call to method piggy and this println statement will always print the original value of sName (I guess , the case of passing Variables by value) I hope , I am correct.? Thanx
Gaja Venkat
Ranch Hand
Joined: Aug 10, 2001
Posts: 50
posted
0
Hi: I would like to answer the queries by explaining the entire flow in detail: The code is as follows
Let us examine the flow. 1. sName is initialised to "vandeleur" 2. t is created. 3. t.piggy(sName); is executed Here the parameter name of piggy method is also sName. But inside the piggy method, any occurence of sName actually refers to this parameter of piggy method (ie to the local variable sName of piggy method) and not to the static class variable sName at line1. Now, when t.piggy(sName) is called, the reference to the String literal created at line 1 is passed to the local sName of piggy method. So now both the sName variables refer to the same location. But inside the piggy method, the local variable sName is modified as follows: 4. sName = sName + "wiggy"; Note that this modification actually changes the reference of the local variable sName. This is because strings are immutable. That is, once created strings cannot be changed. So what happens is that, a new String literal "vandeleur wiggy" is created in the String pool and the local variable sName starts pointing to this, ie the reference of local sName gets changed. Hence this does not affect the static class variable sName and it still points to the string literal "vandeleur". The point is that when a reference is passed to a method, any change done to the object referred to by the reference will get reflected in the calling method. But any change done to the reference itself will not get reflected in the calling method. Here because of the immutability of Strings, though it appears like we are modifying the original String literal value in piggy method, actually it is the reference that gets changed. If there was no start() in piggy, then the println statement in main would print "vandeleur". Hope that is clear. Now let us see what happens with start() 5. start(); When this is executed, actually a registartion of a new thread occurs with the Thread scheduler. And it is this scheduler that determines when the thread will be executed. (This in turn is based on the underlying scheduling technique). When this thread gets its turn to execute, then its run method will be executed. And inside run method after the for loop has finished execution, the static class variable sName will be pointing to a new string literal "vandeleur 0 1 2 3". But the BIG question is when will this thread exectuion happen? This is purely at the discretion of the thread scheduler. There are two possible scenarios; 1) The thread gets to execute before the println in main method. In this case, as discussed above, after run executes, the static class variable sName would be pointing to "vandeleur 0 1 2 3" and the println in main method would print "vandeleur 0 1 2 3" 2) The thread gets to execute after the println in main method. In this case, the static class variable sName would be pointing to "vandeleur" and that gets printed out.
This is the reason why the answer is option 4, that is, Compilation and probably output of "vandeleur" but possible output of "vandeleur 0 1 2 3" ****************************************************************
So the output can also be: "0 1 2 3" or "0 1 2 3 vandelur" or "vandelur" or "vandeleur 0 1 2 3" Any comments, --Farooq
{/QUOTE] There is no way that "0 1 2 3" or "0 1 2 3 vandeleur" will be printed. Inside the for loop, after each execution, the sName will start pointing to "vandeleur 0", "vandeleur 0 1" etc and at the end of the for loop, it will point to "vandeleur 0 1 2 3".
Hi all, I am always getting the output of "vandeleur". This means "run()" method is not executed at all,isn't it? But WHY?? Can Someone help clear my confusion... Thanx
The run method does get executed, but only thing we don't know when, as it is decided by the thread scheduler. To confirm this, please modify the run method as follows & run and see. public void run(){ for(int i = 0;i < 4; i++){ sName = sName + " " + i; } System.out.println("From run method - SName is "+sName); } You will find that "From run method - sName is vandeleur 0 1 2 3" will be printed, irrespective of whether the println in main method prints "vandeleur" or "vandeleur 0 1 2 3". Hope this explanation is clear. Gaja
Samira Mastali
Ranch Hand
Joined: Aug 03, 2001
Posts: 74
posted
0
Hi Gaja, Thanks for your wonderful answer and explanation. Regards, Samira
Mateen Nasir
Ranch Hand
Joined: Aug 01, 2001
Posts: 33
posted
0
Thanks a lot for such a great explanation
Gaja Venkat
Ranch Hand
Joined: Aug 10, 2001
Posts: 50
posted
0
Hi: Thanks for your comments! Welcome!! Regards, Gaja