Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running the run method directly on a Thread

 
Massimo Battestini
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why this code prints out hello?
I was expecting the 'run' method of the Thread class to run (which won't print anything) and not the run method of TestThread4; in fact I'm running the 'run' method on a new Thread instance....

Any idea?

 
Henry Wong
author
Marshal
Pie
Posts: 21507
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was expecting the 'run' method of the Thread class to run (which won't print anything) and not the run method of TestThread4; in fact I'm running the 'run' method on a new Thread instance....


The run() method of the Thread class, actually does stuff. It is called by the new thread that is created by the start() method. And if you don't override it, the run() method looks for a runnable object that has been passed to the Thread object via the constructor and call its run() method.

In this case, your main thread called it directly, but it still does the same thing -- look for the runnable passed via the constructor and call its run() method.

Henry
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the description of run() method in the api: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html
void run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.


Thread class implements Runnable, so new Thread4() creates the Runnable object.
You pass this Runnable (TestThread4 object) to the new Thread instance (second Thread), so if you call the run() of this second instance,
it calls run() of the Runnable passed to it's constructor - TestThread4.run().
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code to what Henry said would look like this



So if you use your custom Thread sub-class, then results might not be as expected



Here output would be a and not hello...
 
Hukm chand
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one doubt.
In line 9 you invoking constructor of MyThread which don't have single argument constructor.
how this would print a.
i got 2 error while executing this above code.
please help me and clear my doubt.
you made one mistake I learned that one source file there will be one public class and it would be class containing main method.
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Ankit forgot the constructor?



output: a
 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I learned that one source file there will be one public class and it would be class containing main method

The last is wrong. A file can contain only one public class, and the name of the file must match the public class name if any. But another class can contain the main method. In fact, ALL the classes in a file can contain a main method and you can execute anyone of them after compile the .java file.

Greetings.
 
Hukm chand
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. David for u'r help.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Wheeler wrote:I think Ankit forgot the constructor?


You got me
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
Bob Wheeler wrote:I think Ankit forgot the constructor?


You got me

Yeah. I feel like I can get 102% in the SCJP exam. LOL

cheers
Bob
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic