• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running the run method directly on a Thread

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Ankit forgot the constructor?



output: a

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. David for u'r help.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic