aspose file tools*
The moose likes Beginning Java and the fly likes Thread class and Runnable interface 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 » Java » Beginning Java
Bookmark "Thread class and Runnable interface" Watch "Thread class and Runnable interface" New topic
Author

Thread class and Runnable interface

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi all

When do we create a thread instance using a thread class and when do we implement the Runnable interface? Which one is preferred and why?


~ Mansukh
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
Start here: https://www.coderanch.com/how-to/java/ExtendingThreadVsImplementingRunnable


Ping & DNS - my free Android networking tools app
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Both are the ways of creating a thread in java, but we prefer Interfaces over inheritance because it gives you flexibility to extend other class you might want.

I will extend Thread class only when my class has to be a thread only.(IS-A).

My class has to do other things beside being a thread like calculations, calling other interfaces, joining results then i will better choose Interface.Hope that will help you.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157


Thanks man.. Respect..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread class and Runnable interface