Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread

 
laxmidhar prad
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the different between Thread & runnable interface.Among two which one is best
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please choose the forum more carefully.

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

Dave
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/view?ExtendingThreadVsImplementingRunnable
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic