IntelliJ Java IDE
The moose likes Threads and Synchronization and the fly likes two ways of creating the threads Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "two ways of creating the threads" Watch "two ways of creating the threads" New topic
Author

two ways of creating the threads

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 200

Hi ,
As I was going through threads I just want to know the process of creating the threads is two way either by extending thread class or by implementing runnable interface, My query is that why the java have provided the two ways , I am doing lot of googling for that ,I just wanted to know what's the technical reason behind introduing the two ways..!!
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 343

Hi,

There just two different approaches both of which have been superseded to some extent. Implementing runnable is usually the preferred choice one reason for which is inheritance i.e. If you have sub classed thread you can't inherit from anything else (multiple inheritance), I guess if you were a beginner you might find the concept of a Thread class more natural and its a wrapper for other methods i.e. it represents a thread rather than a unit of work so it does have its uses.

You should probably me more looking at Executors / Tasks etc in modern java.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2329
Also summarized at http://www.coderanch.com/how-to/java/ExtendingThreadVsImplementingRunnable
 
 
subject: two ways of creating the threads
 
Threads others viewed
Regarding Synchronization
Using System properties or new Properties object?
Why does this happen?
Passed SCPJ2 With 93%
System.properties or new properties object for reading a .properties file?
developer file tools