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

Simple thread question

 
Matt Drexl
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11865
194
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And now it is there.

Regards, Andrew
 
Nikos Pougounias
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()";
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic