aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread, Runnable and their run() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread, Runnable and their run() method" Watch "Thread, Runnable and their run() method" New topic
Author

Thread, Runnable and their run() method

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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~!

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Aug 03, 2008
Posts: 9321
    
  17

Did you start the thread??
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ankit Garg wrote:Did you start the thread??






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

Thanks Ankit.....
Tebogo Maitsapo
Greenhorn

Joined: Mar 26, 2010
Posts: 3
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
Unmesh Chowdhury
Ranch Hand

Joined: Jun 20, 2010
Posts: 45
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:



M.Sc. in CS, OCPJP6 93%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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

Joined: Jun 20, 2010
Posts: 45
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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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

Joined: Jun 20, 2010
Posts: 45
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread, Runnable and their run() method