wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Please help regarding creating a thread object without actually extending thread class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Please help regarding creating a thread object without actually extending thread class" Watch "Please help regarding creating a thread object without actually extending thread class" New topic
Author

Please help regarding creating a thread object without actually extending thread class

Kay Kingston
Greenhorn

Joined: Sep 04, 2012
Posts: 8
Can someone please explain how the Thread object is created without actually extending thread class in the following code


harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Hi Kay,

You have two ways to create a Thread in Java
1. Extend Thread Class
2. Implement Runnable interface

in both scenarios you need to override public void run() method.

You have chosen here 2nd way.

public class RunnableTest extends Addition implements Runnable

also you got public void run() from superclass. Got it??
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

Hi Kay Kingston,

Welcome to CodeRanch!

I hope Harshvardhan's answer will be helpful to you.

Also, please make a practice to UseCodeTags in your code. It makes reading of code much easier.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Kay Kingston
Greenhorn

Joined: Sep 04, 2012
Posts: 8
harshvardhan ojha wrote:Hi Kay,

You have two ways to create a Thread in Java
1. Extend Thread Class
2. Implement Runnable interface

in both scenarios you need to override public void run() method.

You have chosen here 2nd way.

public class RunnableTest extends Addition implements Runnable

also you got public void run() from superclass. Got it??


Hey thanks for the quick reply
Do you mean run() method is inherited from Addition into RunnableTest by your last statement?
Kay Kingston
Greenhorn

Joined: Sep 04, 2012
Posts: 8
Anayonkar Shivalkar wrote:Hi Kay Kingston,

Welcome to CodeRanch!

I hope Harshvardhan's answer will be helpful to you.

Also, please make a practice to UseCodeTags in your code. It makes reading of code much easier.


Hi thanks
I will
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Kay Kingston wrote:
harshvardhan ojha wrote:
also you got public void run() from superclass. Got it??


Hey thanks for the quick reply
Do you mean run() method is inherited from Addition into RunnableTest by your last statement?

Yes, that is correct. And doing so fulfills the Runnable interface's requirements. But it isn't really good practice since it makes it harder to associate the implemented method (run()) with the interface that requires it (Runnable). Either Addition should implement Runnable since it has the run() method, or the run() method should be moved to RunnableTest class since it implements Runnable. Either way, the 'implements Runnable' and run() method should be in the same class to keep things easy.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help regarding creating a thread object without actually extending thread class
 
Similar Threads
2 thread instances using the same Runnable object
Regarding Threads
Thread program not compiling
When both Thread and Runnable have public void run
doubt in threads