*
The moose likes Beginning Java and the fly likes thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "thread" Watch "thread" New topic
Author

thread

laxmidhar prad
Ranch Hand

Joined: Feb 05, 2006
Posts: 33
what is the different between Thread & runnable interface.Among two which one is best
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Please choose the forum more carefully.

I'm moving this to Java in General (beginner)

Dave
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
http://java.sun.com/j2se/1.5.0/docs/api/index.html

As you can see Thread implements Runnable. You are going to need to override run(), and you use run() when you implement Runnable, so no difference there. You can even call the non-static methods of Thread if you need to inside your Runnable, but you need to call Thread.currentThread(), so you aren't losing any functionality by not using the Runnalbe interface instead.

extend Thread and you are stuck, you can't extend another class if you need it. So just implement Runnable. Starting the thread is also slightly different.

[ August 16, 2006: Message edited by: Rusty Shackleford ]
[ August 16, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Most of us figure Thread implementing Runnable was a mistake and extending Thread is (almost?) always a bad idea. We tend to use Runnable, TimerTask, FutureTask and the like.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
http://faq.javaranch.com/view?ExtendingThreadVsImplementingRunnable


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I have few comments over this point:

1) Interface Runnable have only 1 method which you mandatory to implement.
2) If you need other methods like suspend() resume() sleep() join() yield() and stop() then
go for extending class Thread
3) Extending the Thread class will make your class unable to extend other classes, because of the single inheritence feature in JAVA.
4) If you want to execute run() method for multiple no of times then its better to you Runnable.

public class testing implements Runnable {

public void run() {
System.out.println("Hello Run --->");
}
public static void main(String args[]){

testing testing = new testing();
Thread thd = new Thread(testing);

thd.run();
thd.run();
}
}

While Thread class doesn't allow yo to call the start() methods more than once.
will throw IllegalThreadStateException.

5) Thread Class actually implements Runnable interface internally.
Ksh Aggarwal
Greenhorn

Joined: Sep 16, 2009
Posts: 28
Thread class & runnable interface are the same.the difference is just that of a class and an interface and usage depends on design decision.

Objects of class implementing runnable don't act as threads. they have to b given to Thread objects which actually spawn a new thread.
for general usage we put the required code block in a Runnable implementing class. Now this becomes service which needs to b executed. its object is then given to actual Thread object to create a thread and run this service.

Extend thread only when you want to add functionality to Thread class
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Prabhat Ranjan wrote:I have few comments over this point:

Was it really necessary to kick this old thread with an exact copy of your post in this thread? And please read my reply in that thread as well. I'm not going to copy it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
i have read your reply 1st in my question ...Thanks for reply..

Actually i was not necessarily to copy from my Q to this Q answer.

Any way..actually this question is very popular now a days and everyone ask about this which 1 to use when and why ?

lots of douts i had now clear.

if you know more please share with us.
 
 
subject: thread
 
Similar Threads
Tech Word Game
subject
WA #1.....word association
Thread and Object
Thread & Synchronization