aspose file tools*
The moose likes Threads and Synchronization and the fly likes a loop in a thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "a loop in a thread?" Watch "a loop in a thread?" New topic
Author

a loop in a thread?

feng lee
Ranch Hand

Joined: Oct 29, 2002
Posts: 33
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

Joined: Feb 23, 2001
Posts: 104
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

Joined: Oct 29, 2002
Posts: 33
In which condition will these codes output "vandeleur"?In which condition wil these codes output "vandeleur 0 1 2 3"?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

"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

Joined: Jan 30, 2000
Posts: 18671
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
 
subject: a loop in a thread?
 
Similar Threads
Thread question
Threads Q
about thread
Question 19 from Green's Mock
output of the thread