• 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

Marcus Green Mock- doubt

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi group,
Can any one please explain how the ans is 4,
I thought the ans is 1. Because I thought that we can call start() only thru a Thread object.

///Q.19
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 output of either "vandeleur", "vandeleur 0", "vandeleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3"
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ January 26, 2006: Message edited by: Brandon Tom ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sudhir,

when calling t.piggy(), you actually call the start() method on a thread object (on t). instead of start(), it could have said this.start(), where this refers to t.

brandon, which result did you get? would be interesting! (i got one of the expected results)

greetings
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sudir,

The output can`t be exaclty determined, the fourth answer is correct.



Sorry for a little confusing explanation but my english sucks.

It goes like this.... we start in main at line 3, then 4, 5, then there`s a method invocation so we go to 7, 8, 9... and here`s the place from which we don`t know where the program will go to. And a small note just in case, in the piggy method, static field of type String declared and initialized in Tux class will not change. Local variable sName is a copy of static field, since it has the same name as the static field declared in Tux class and there`s no explicit refrence this (even though it`s a static member) like "this.sName = sName + "wiggy"; in this method local variable sName shadows the static field and we are making operations on local variable all the time, it dies when the metod dies, static field remains the same.


OK, let`s go back to the main problem. Assume it ends invocation and goes back to main.

Now it can execute line 6 and print "vandeleur", then move to thread of execution t, and do the for loop, but there`s no another print statement to print static String sName variable, so the output is "vandeleur".

Or after invocation of t.piggy(sName) at line 5, it could move to thread of execution t, start for loop and for example after one iteration come back to main and print the static String sName variable which would be then "vandeleur 0" then move back to thread of execution t and finish the loop.... or it could be after two iterations, then "vandeleur 0 1" or after three iteration... etc.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In support to Aleksander, answer.

To get an output(rather force) other than what is printed in main method

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aleksander,

Threads are a bit confusing for me.

OK, let`s go back to the main problem. Assume it ends invocation and goes back to main.

Now it can execute line 6 and print "vandeleur", then move to thread of execution t, and do the for loop, but there`s no another print statement to print static String sName variable, so the output is "vandeleur".



Could you please clarify how after the method invocation and after line 9 the for loop is not completely executed and why the output is not "vandeleur 0 1 2 3" only .
Second question, how can after thread t has started we move back and forth between the for loop and the main?

Thanks for your time.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is "vandeleur":
Because you are modifying String object in Run() method..But the you are not printing anything there..So your original object will not be modified..If you are printing inside the run() method ,the fourth answeris correct one.
"Strings are immutable"
 
sudhir harsha
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Aleksander,

ThankU for your explanation.

regards,
Sudhir
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rammmmm reddy:
The answer is "vandeleur":
Because you are modifying String object in Run() method..But the you are not printing anything there..So your original object will not be modified..If you are printing inside the run() method ,the fourth answeris correct one.
"Strings are immutable"




But the string object is a static member of the class instead of a local variable in main. A bit hard to see with the way the code is written.
 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rammmmm reddy:
The answer is "vandeleur":
Because you are modifying String object in Run() method..But the you are not printing anything there..So your original object will not be modified..If you are printing inside the run() method ,the fourth answeris correct one.
"Strings are immutable"



This is not true. Please read my explanation below.



Originally posted by Faqeer Khudaka:
Hi Aleksander,

Threads are a bit confusing for me.

Could you please clarify how after the method invocation and after line 9 the for loop is not completely executed and why the output is not "vandeleur 0 1 2 3" only .
Second question, how can after thread t has started we move back and forth between the for loop and the main?

Thanks for your time.



Hello Faqeer,

I`ll try to explain. When you invoke start() method on a Thread object, Thread object becomes a thread of execution and moves to runnable state, what means it is ready to run, but not running and therefore it does not mean it will run exactly in the same moment you invoked start() method, it just mean it might run but we don`t exactly know when it will happen, it`s up to scheduler. That`s why the for loop may not be executed before print statement in the main method. On the other side it can iterate one, two, three or four times and sometime between program execution can move back to the main method() and print what sName reffers to in this moment. The puropse of threads is to work concurrently, or better to say wee see it like they worked concurrently, because proccessor can handle only one process at a time. The thread "t" must be able to move back to runnable state(not running) to allow main method to execute, so we can see the whole proccess as concurrent proccess, because imagine situtation when loop in run() method was to iterate 10000000000 times, if it wasn`t possible to move to main method in any time, main method would have to wait until 10000000000 iterations finish, or even more main method would have to wait until thread "t" dies. What if there was many, many more other things to execute in the "t" thread that would take a long long time? Main method would be waiting and waiting and waiting.... then there`s no concurrency. So if you have two threads, in our case one thread is a main method, second thread is "t", execution can move back and forth between them and in our case we cannot say when moving from one to another will exactly happen.

Hope this didn`t sound too confusing and sorry for my English.
 
Faqeer Khudaka
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Aleksander,

You have cleared "clouds of confusion" that I had on threads.
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic