File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a loop in a thread?

 
feng lee
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
raimondas zemaitis
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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"?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic