Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Problem

 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,i cann't figure out why the thread seems not to work properly because the console only put out the String "vandeleur" no more,no less? :-)
public class Egg extends Thread{
static String sName = "vandeleur";
public static void main(String argv[]){
Egg t = new Egg();
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;
}
}
}
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on what you mean by "properly." If it doesn't work the way you expect it to work, then you have invalid expectations.
First, there is only one println() in the whole program, so the most it can print is one line.
What do you expect that this line should print?
[ February 08, 2002: Message edited by: Rob Ross ]
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all I am surprised that you were able to compile and run your program? Java does not allow a non-static method like piggy(...) to be called from a static method
If method piggy(...) was declared static, and the program compiled and run, then from your code you should only get one output of string "vandeleur", in other words there is nothing wrong with your program!
First, you can't modify a string which is passed to method piggy() because it's a copy of the reference to a "immutable" string object! So the copied reference "sName" gets modified not the original reference you passed to the piggy() method from inside main()
Second, the class field sName may or may not get modified by the time the statement "System.out.println(sName);" gets reached.
It might help if you state what you are trying to do with your thread program.
[ February 08, 2002: Message edited by: Rajinder Yadav ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajinder,
the method invocation t.piggy() is perfectly legal from main(). He's not calling piggy() from main(), he's invoking the method piggy() on the object in reference variable t.
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob you're right! I guess my eyes glossed over that sneaky t
 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Answer:vandeleur 0 1 2 3
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard you're right, also the answers could be "vandeleur 0", or "vandeleur 0 1" or "vandeleur 0 1 2"
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or also just "vandeleur".
The point is, you can't guarantee anything about thread scheduling, so any of these combinations is perfectly possible.
 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:
Or also just "vandeleur".
The point is, you can't guarantee anything about thread scheduling, so any of these combinations is perfectly possible.

I disagree.When the start() method are called,a thread then will be scheduled to run() at anytime.
We can not decide the time when the thread will be running.But definitely, the thread will run
at sometime,right?
 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"JVM creates one user thread for running a program. This thread is called main thread. The main method of the class is called from the main thread. It dies when the main method ends. If other user threads have been spawned from the main thread, program keeps running even if main thread dies. Basically a program runs until all the user threads (non-daemon threads) are dead."
Can you tell me whether the description about thread is right?(I quote from somebody's note)
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Richard Wilson:

We can not decide the time when the thread will be running.But definitely, the thread will run
at sometime,right?


For someone who is disagreeing with me, you sure sound like you're agreeing with me
After method piggy() is invoked, at some point another thread will be spawned, after the start() method has been called. But you cannot predict exactly when that thread will start. It could start *after* main() has ended. Or it could start, run, and complete before piggy() returns control to main(). My point is, you cannot guarantee the order of execution, so therefore you cannot predict what gets printed in this example.
 
Richard Wilson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:

For someone who is disagreeing with me, you sure sound like you're agreeing with me
After method piggy() is invoked, at some point another thread will be spawned, after the start() method has been called. But you cannot predict exactly when that thread will start. It could start *after* main() has ended. Or it could start, run, and complete before piggy() returns control to main(). My point is, you cannot guarantee the order of execution, so therefore you cannot predict what gets printed in this example.

That's Okay.I misunderstood your previous explanation.
Now, I get it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic