Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Running the run method directly on a Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Running the run method directly on a Thread" Watch "Running the run method directly on a Thread" New topic
Author

Running the run method directly on a Thread

Massimo Battestini
Greenhorn

Joined: Nov 21, 2008
Posts: 11
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Hukm chand
Greenhorn

Joined: Feb 20, 2009
Posts: 16
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.

preparing for SCJP 6
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
I think Ankit forgot the constructor?



output: a


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
David Marco
Ranch Hand

Joined: Feb 23, 2009
Posts: 44
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.


SCJP 6
Hukm chand
Greenhorn

Joined: Feb 20, 2009
Posts: 16
Thank you. David for u'r help.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Bob Wheeler wrote:I think Ankit forgot the constructor?


You got me
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running the run method directly on a Thread