• 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 loop in a thread?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>Compilation and output of "vandeleur wiggy"
2>Compilation and probably output of "vandeleur" but possible output of "vandeleur 0 1 2 3"
the answer is 2>,why?
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
piggy() will return faster than thread will be constructed after start() and thus will print current value of sName. sName in the piggy() is local to that method since parameter passed in is with the same name. In order to get "wiggy" added "this" should be used (this.sName = this.sName + sName).
 
feng lee
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which condition will these codes output "vandeleur"?In which condition wil these codes output "vandeleur 0 1 2 3"?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on the thread scheduler. Once start() is called within piggy(), a new thread is started - now it's a race between the new thread and the original one to see what happens next. If the original thread executes

before the new thread executes

then
vandeleur
is printed; otherwise you may see
vandeleur 1
or
vandeleur 1 2
or
vandeleur 1 2 3
depending on how many times the new thread executes the loop before the print statement is executed. It's impossible to guarantee what will happen here.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"vandeleur" will be the only output ever.
1. Start() returns immediately so the print statement will occur before the thread even gets started. This is all but guaranteed.
2. Wiggy is only ever found inside of the local variable in the piggy method. it will never affect the static variable of the same name.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is all but guaranteed.
True - all but. Perhaps it is indeed always true on current JVMs, but it's not specified anywhere as far as I know. It would be perfectly possible for someone to make a JVM which immediately yields to a thread that has just been started, I think. As such I believe the original answer is best: probably output of "vandeleur" but possibly output of "vandeleur 0 1 2 3".
[ November 06, 2002: Message edited by: Jim Yingst ]
reply
    Bookmark Topic Watch Topic
  • New Topic