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

Thread

 
Arathi Raj
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ronnie Ho
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic