• 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

Thread, Runnable and their run() method

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this code....



The Runnable interface defines an abstract run() method and the Thread class implements the interface, so has its own concrete run() method. However the Thread class implementation of run() is different from most others because of its special use in the creation of new threads.

The Thread run() method has no “worker” code of its own, it just calls the run() method of the Runnable object that was passed to its constructor.

To instantiate a Thread object we normally pass a Runnable class to the constructor as the “target” of the thread. When you call the Thread's start() method it creates a new thread whose first and only action is the to call Thread class' own run() method once. The Thread class run() calls the run() method on the target Runnable, which does the work of the thread.



Is this above para correct? If so, what is the problem with the above mention code? I've this doubt for a long time....

Thanks in Advanced~!
 
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
This is how the code in Thread class looks
So basically, when you call Thread.start() the run method of Thread class is called and it calls the run method of the Runnable passed to the Thread class constructor...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:Check this code....




That's why I asked it. This code won't give any output.. But I think it should print It's name.

Where is the problem?
 
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
Did you start the thread??
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Did you start the thread??







I forgot..................................

Thanks Ankit.....
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

Ankit Garg wrote:Did you start the thread??







I forgot..................................

Thanks Ankit.....



Sorry ranchers, my first post. Forgetting to start a thread makes this the funniest thread. I did this before and kicked myself whole week. Nevertheless, lets be disciplined and careful programmers
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr. Ankit Grag,
You have discussed the internal chaining of starting of a thread of execution when we create a thread from a Runnable object. But what will be the internal chaining of starting of a thread of execution when we create a thread by extending it from the java.lang.Thread class, for instance:

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Unmesh Chowdhury wrote:Hi Mr. Ankit Grag,
You have discussed the internal chaining of starting of a thread of execution when we create a thread from a Runnable object. But what will be the internal chaining of starting of a thread of execution when we create a thread by extending it from the java.lang.Thread class, for instance:



Hi,
Ankit mentioned in the above his coding is the internal structure of the Thread class in the JAVA API, not about the target object or our own Thread(in your case MyThread). And if you are creating your own Thread, you've to implement the run() method in which way you want to continue.. ( I mean, whether to run on the target object run() method or your Thread's run() method....
 
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

Unmesh Chowdhury wrote:You have discussed the internal chaining of starting of a thread of execution when we create a thread from a Runnable object. But what will be the internal chaining of starting of a thread of execution when we create a thread by extending it from the java.lang.Thread class


When you extend the Thread class, your run method will be called by start method directly. The internal chaining thing comes into play if we override the Thread class, provide a constructor which takes a Runnable as argument and we don't call run on the Runnable in our run method
In the above code the run method of our Runnable anonymous class won't be called. This is because when we start MyThread, run method of MyThread will be called and from it we don't call the run method of the Runnable passed to the constructor...
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mr. Garg. I did also think so but let me clear. That is, automatic internal chaining is comes into play if we create our thread object directly from the java.lang.Thread class. For instance,



In this case, there is a scope to do so. Since otherThread object is stored into t instance as Runnable. Am I in the right direction?
 
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

Unmesh Chowdhury wrote:automatic internal chaining is comes into play if we create our thread object directly from the java.lang.Thread class.


Yes. If we pass java.lang.Thread class a Runnable, then on starting the Thread, Thread class calls the run method of our Runnable. We can implement this ourselves too
 
Unmesh Chowdhury
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mr. Garg. It is the manual way for implementing the internal chaining and this design was also in my mind but your code makes me more sure. Thanks once again.
 
I do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic