File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Thread 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 "About Thread" Watch "About Thread" New topic
Author

About Thread

Narasimha Reddy
Greenhorn

Joined: Oct 05, 2004
Posts: 11
hi,
Thread can be created by extending the Thread or by Implementing the Runnable interface.when these r used. Which most advantage than other.

Rerards,
Narasimha


Narasimha Reddy
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Narasimba, please change your displayed name to conform with our JavaRanch Naming Policy.

It's just a matter of using two names: <first name> <family name>.

Thanks,
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
"Thread can be created by extending the Thread or by Implementing the Runnable interface.when these r used. Which most advantage than other."

It depends on what you want to do. In most cases you will want to implement runnable. As a general rule you extend Thread when you need to add additional behavior to the Thread class: class MyThread extends Thread. Note two things about this approach. First, MyThread is-a Thread, and second, MyThread cannot extend any other class besides Thread. MyThread will then likely be used to start a Runnable object.

As to which is the most advantageous, if you need to add additional capabilities to Thread to create your own thread class, then you will need to extend Thread. If you want to, as will be the overwhelming majority of cases, simply create other threads of execution, then implement Runnable.
Suneesh Raman
Ranch Hand

Joined: Jun 13, 2002
Posts: 42
One more difference I read some where
If ur extending the thread the class is not able to extend any other class .So u r limiting that particular class since java does not suppo[rt multiple inheritance If ur implementing the Runnable interface the class still can extend another class

Suneesh
seemapanth Joshi
Ranch Hand

Joined: Nov 28, 2004
Posts: 47
Check out http://www.jchq.net/certkey/0701certkey.htm !
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The biggest knock against extending Thread is that you are only allowed to extend one class in Java and, if you extend Thread, you're out of luck as far as extending anything else goes. I do, however, use this approach from time to time.

A benefit (or hindrance) with implementing Runnable is the ability to share data. It's quite possible to create a single instance of a Runnable object and spawn multiple threads using that single object. If that Runnable object contains data members, that data would be shared by all of the threads using it. Perhaps that's what you want. Perhaps not. That's up to you.


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Thread