• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a strange question with Thread

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below question comes from examulator.com ...
what happens if the code below is compiled and run...
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;
}
}
}

The answer is:
Compilation and probably output of "vandelur" but possible output of "vandeleur 0 1 2 3"
hmmm... is that right? wouldnt it be more like a possibility of "vandaleur 0", "vandaleur 1", "vandaleur 2", or "vandaleur 3"?
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jasper, it is a good catch. But the main point in the question is
[CODE]
public void piggy(String sName){
sName = sName + " wiggy";
start();
[CODE]
wiggy is never appended to the output as we are modifying the local variable instead of the static variable.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, good point Sarma
but what do you think about the iterator loop adding up 'i',,, in the way that it does it, i do not understand how "vandeleur 0 1 2 3" itself could be a possible output. seems like a bug in the answer. but yes, the question itself is a good one!
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer would only be vandaeleur. There won't be any number appended to it. There is no other possible output here. Sarma rightly mentioned that we are looking at the static variable and not the local one.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jasper
When I added the code shown below, which gave the other thread a chance to finish before the println() in the main thread was executed, the output was:
vandeleur 0 1 2 3

The possible outputs of this program are as follows, each line representing a possible result:
vandeleur
vandeleur 0
vandeleur 0 1
vandeleur 0 1 2
vandeleur 0 1 2 3
sName = sName + " " + i; keeps adding to the end of the string, so the output you suggested is not possible.
I wonder where Marcus Green gets the names he
uses in his examples :roll:
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah ... vandaleur ... like a cross between vandal and voyeur...
OK here is my limited understanding of what is going on...
public class Tux extends Thread{
static String sName = "vandeleur"; // does not
// refer to any instance.
public static void main(String argv[]){
Tux t = new Tux(); // new tux object
t.piggy(sName); // calls piggy method with copy
// of reference to sName as the String parameter
// start insert code
try {
Thread.sleep(100);
}
catch (InterruptedException e)
{
System.out.println(e);
}// which is the 'other' thread that is given a
// chance to run while this one sleeps? Is there
// a 'default' main thread?
System.out.println(sName); // When does this get
// done?
}
public void piggy(String sName){
sName = sName + " wiggy"; // Here there is no
// appending of wiggy to sName
start(); //HOW does this effect when the
// System.out.print call occurs?
}
public void run(){
for(int i=0;i < 4; i++){
sName = sName + " " + i; // If given a chance,
// sName WILL have a space and int i appended to
// it here. Why, is it to do with thread rules?
}
}
}
thanks for help so far
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jasper
Tux is a Thread (it extends Thread), so the start() method will "Cause this thread to begin execution; the Java Virtual Machine calls the run method of this thread" (API docs). So now there are 2 threads to consider - the original thread that is executing the main() method, and the new thread, which is executing the run() method.
The fact that Tux is a Thread does not affect how the String is altered by the code in the for loop. I have simplified the original code so that run() is called directly from main(). Once you see that the output of this version is "vandeleur 0 1 2 3", you can start considering how the threads executing independently make the orginal code different from this code.

Unrolling the loop:
static String sName = "vandeleur";
...
sName = sName + " " + 0; // sName is now "vandeleur 0"
sName = sName + " " + 1; // sName is now "vandeuler 0 1"
This is just string concatenation.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This question sure is interseting.
I had a questions:
When the run method is executed, doesn't the for loop complete and give the output of vandeleur0123.
It will be great if any of you could answer that.
Pallavi
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much John! Hey Pallavi, if you are wondering if the most recent bit of code outputs vandaleur 0 1 2 3 ... well, it does!
A small point John - strictly speaking, i guess one should really call t.start() rather than t.run() ?
After comparing your and the original code, i surmise:
Concerning the fact the System.out.print call is in the initialisation code, the code is part of the class instantiation, but that is part of a thread, which must share time with the other thread that is called from piggy method?

Originally posted by John Paverd:
Jasper
static String sName = "vandeleur";
...
sName = sName + " " + 0; // sName is now "vandeleur 0"
sName = sName + " " + 1; // sName is now "vandeuler 0 1"
This is just string concatenation.


Oh, ahem, yes! haha .. but in the original code up the top, there was a bit of string concatenation going on with the code
public void piggy(String sName){
sName = sName + " wiggy";
but that concatenation did not occur. just confuses me why it can happen at the end there?
[ January 26, 2003: Message edited by: Jasper Vader ]
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jasper Vader:
A small point John - strictly speaking, i guess one should really call t.start() rather than t.run() ?


Yes, one should call the start() method, not the run() method. I did that so that you could see what the thread would do without actually starting a new thread.


but in the original code up the top, there was a bit of string concatenation going on with the code
public void piggy(String sName){
sName = sName + " wiggy";
but that concatenation did not occur. just confuses me why it can happen at the end there?


2. The concatentation does occur, but the only thing that is modified by this method is the argument String reference sName, which is a copy of the static String reference sName. Assigning a new String to the argument reference does not change the original reference, as Sarma Lolla pointed out.
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having got the mechanics out of the way, let's consider the full problem:
The main method calls piggy(), which starts a new thread. The new thread will execute the run() method. The run method modifies the static String reference sName. After the main method has called piggy(), the main method prints out the String that sName refers to.
So what does sName refer to when println() in main() is executed? We can't exactly be sure.
Did the JVM move the thread running main to the runnable state, and allow the new thread to complete before running the main thread again?
Then the output would be vandeleur 0 1 2 3
Did the JVM complete the execution of the main method before running the new thread?
Then the output would be vandeleur
Is this code running on a computer with multiple processors, so that both threads can run in parallel?
Then the output could be vandeleur, or vandeleur 0, or vandeleur 0 1, etc.
There are other possible combinations, so the only thing that we know for sure is that the output will be at least vandeleur, because that is what sName referred to before the other Thread was started.
HTH
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks John, this helps a lot - particularly this...

Originally posted by John Paverd:
Did the JVM move the thread running main to the runnable state, and allow the new thread to complete before running the main thread again?
Then the output would be vandeleur 0 1 2 3
Did the JVM complete the execution of the main method before running the new thread?
Then the output would be vandeleur


So it is quite possible that while the main Thread is running, when it encounters a call to a method and that method starts a new thread, it may automatically yield and allow that new thread to complete - or not ...
And concerning sName, Piggy does not affect the static sName, as in its constructor it receives a String, and that String was sName, so it was passed a copy of a reference to sName which it modifies without effect - as opposed to run() method, which does not get passed a copy of the reference to sName, so when it changes sName the actual changes are visible in the original static sName. (i thought originally that sName's change was visible only because it was in the run() method).
thanks again John
 
Pallavi Chakraborty
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everybody,
Thanks for a great explanation. There is one point though. I am wondering about what happens to the method piggy.
The concern stems from the following.
Consider this code from Heller/Roberts book.
class TestThread3 extends Thread
{
public void run()
{
System.out.print("Running");
System.out.print("Done");
}
private void xxx()
{
System.out.print("In xxx");
}
public static void main(String args[])
{
TestThread 3 ttt = new Testthread3();
ttt.xxx();
ttt.start();
}
}
The question is "If you attempt to complie and
execute the following application, will it ever print - In xxx ?"
The answer is Yes.
What do you guys think.
Thanks a lot again.
Pallavi
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pallavi Chakraborty:
I am wondering about what happens to the method piggy.
The concern stems from the following.
Consider this code from Heller/Roberts book.
...
The question is "If you attempt to complie and
execute the following application, will it ever print - In xxx ?"
The answer is Yes.


The output of the code you posted is
In xxxRunningDone
Are you asking why the example code you posted prints this output?
Do you feel that the call to piggy in the original question is different than the call to
xxx in your example? How so?
I'm sorry I wasn't able to answer your question, but if you explain your question a little more, I will try again.
[ January 28, 2003: Message edited by: John Paverd ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic