wood burning stoves 2.0*
The moose likes Java in General and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
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; }
}}
The output is this
Compilation and output of either "vandaleur", "vandaleur 0", "vandaleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3

Can anyone tell me, why it gives such output. Does thread depends on the underlying operating system.

Thanks
Arathi
Ronnie Ho
Ranch Hand

Joined: Aug 10, 2005
Posts: 47
The output looks reasonable to me, what result do you expect ? Do you expect the "wiggy" String appended ? or the same result everytime ?
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Does thread depends on the underlying operating system.
It depends on the JVM's thread scheduler (upon which you should not depend for consistent behavior).
Tony William
Ranch Hand

Joined: Jun 27, 2005
Posts: 54
correct me if I am wrong.

In the sample code, there is actually one thread running (ignoring the main program thread). As a result, the mentioned result is expected.

In case we have 2 or more thread, then the result will depend on the JVM's thread scheduler (as pointed out by Steve).


MCP, MCP+I, MCSE(NT4), MCSE+I, MCSE(2000), MCDBA, MCSD(VS6)<br />SCJP 5.0, SCBCD 1.3<br />ICED(v5.0), ICSD (WSP5.0)
Ronnie Ho
Ranch Hand

Joined: Aug 10, 2005
Posts: 47
Originally posted by Tony William:
correct me if I am wrong.

In the sample code, there is actually one thread running (ignoring the main program thread). As a result, the mentioned result is expected.

In case we have 2 or more thread, then the result will depend on the JVM's thread scheduler (as pointed out by Steve).


The result is not expected.
Maybe I should say the result is not certain. You can't be sure for how many times the for loop will get iterated before System.out.println is called by the main; it still depends on the JVM thread scheduler.
Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
Thanks for the reply. Can anybody tell me where I can find proper tutorial on Thread?
ARathi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
Doubt in Marcus's question
marcus green test 1 - ques 19
Question 19 from Green's Mock
a strange question with Thread
Thread question