Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Problem" Watch "Thread Problem" New topic
Author

Thread Problem

Richard Wilson
Ranch Hand

Joined: Jan 12, 2002
Posts: 57
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;
}
}
}


Richard Wilson
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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 ]

Rob
SCJP 1.4
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
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 ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Jan 18, 2002
Posts: 178
Rob you're right! I guess my eyes glossed over that sneaky t
Richard Wilson
Ranch Hand

Joined: Jan 12, 2002
Posts: 57
According to the Answer:vandeleur 0 1 2 3
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Richard you're right, also the answers could be "vandeleur 0", or "vandeleur 0 1" or "vandeleur 0 1 2"
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Jan 12, 2002
Posts: 57
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

Joined: Jan 12, 2002
Posts: 57
"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

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Jan 12, 2002
Posts: 57
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Problem
 
Similar Threads
static string
output of the thread
Passing Values
Question based on pass by reference
Thread - Please explain!