aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Simple thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Simple thread question" Watch "Simple thread question" New topic
Author

Simple thread question

Matt Drexl
Greenhorn

Joined: Sep 19, 2008
Posts: 2
Hi all

I'm trying to understand the following code relating to threads:



When it's run, it outputs "Initialised", indicating that the thread's run method is only ever called after System.out.println(str);

Why is this?
Matt Drexl
Greenhorn

Joined: Sep 19, 2008
Posts: 2
Oops, just noticed I've made my first post in the wrong category...

Should be in Programmer Certification (SCJP).
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11508
    
  95

And now it is there.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Nikos Pougounias
Ranch Hand

Joined: Jan 16, 2008
Posts: 110
myTest and main are two different threads. So, "Initialised" is not a guaranteed output. You should specify that main should continue immediately after myTest completes by using the join() method.



Now that's a guaranty!


Nikos' Java blog
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Hi,
Here i tried to explain in comments with code.

public class Test extends Thread {
private static String str = "Initialised";

public static void main(String[] args) {
Test myTest = new Test();
myTest.myFunction(str); //calls myFunction()
System.out.println(str); // str value is "Initialized" only as the changes in below methods won't change the original str as in Java its Pass by value(copy is passed) not pass by reference
}

private void myFunction(String str) {
str = "Did this in myFunction()";
start(); // calls run()
}

public void run() {
str = "Did this in run()";
}
}


hope this helps.. but yes as said ahead its not guaranteed ...
[ September 19, 2008: Message edited by: Pranav Bhatt ]
sannuth kashikar
Greenhorn

Joined: Sep 16, 2008
Posts: 14
hi,

@Pranav Bhatt

public class Test extends Thread {
private static String str = "Initialised";

public static void main(String[] args) {
Test myTest = new Test();
myTest.myFunction(str); //calls myFunction()
System.out.println(str); // "" str value is "Initialized" only as the changes in below methods won't change the original str "" as in Java its Pass by value(copy is passed) not pass by reference
}

private void myFunction(String str) {
str = "Did this in myFunction()";
start(); // calls run()
//System.out.println(str);
}

public void run() {
str = "Did this in run()";
}
}

str in the run method is a static variable which is declared in 2nd statement and when run method runs str will refer to " Did this in run()" object ,since it is a class variable.
uncomment println statement and run 4 to 5 times and will get different outputs.Its upto scheduler which will be executed first,main or run.Hence "Initialised" is not a guaranteed output.
[ September 19, 2008: Message edited by: sannuth kashikar ]

scjp5 90%
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
I have already told it's not guaranteed output. Check my last line of post
hope this helps.. but yes as said ahead its not guaranteed ...

str in the run method is a static variable which is declared in 2nd statement and when run method runs str will refer to " Did this in run()" object ,since it is a class variable.


This makes clear what the person was asking, i must have fallen asleep to think that he's getting confused with the changes in str within myFunction() method

Thanks
[ September 19, 2008: Message edited by: Pranav Bhatt ]
Leonid Shchervinsky
Ranch Hand

Joined: Sep 07, 2006
Posts: 32
Try using Thread.yield();
=========================

public class Test extends Thread {

private static String str = "Initialised";

public static void main(String[] args) throws InterruptedException {
Test myTest = new Test();
myTest.setPriority(MAX_PRIORITY);
//myTest.setPriority(MIN_PRIORITY);
Thread.yield();
//myTest.start();
myTest.myFunction(str);
System.out.println(str);
}

private void myFunction(String str) {
str = "Did this in myFunction()";
start();
}

@Override
public void run() {
str = "Did this in run()";
}
}


<a href="http://www.shchervinsky.com/java/" target="_blank" rel="nofollow">http://www.shchervinsky.com/java/</a>
 
wood burning stoves
 
subject: Simple thread question