Forums Register Login

thread

+Pie Number of slices to send: Send
what is the different between Thread & runnable interface.Among two which one is best
+Pie Number of slices to send: Send
Please choose the forum more carefully.

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

Dave
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
Why is the word "abbreviation" so long? And this ad is so short?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1550 times.
Similar Threads
Thread states in Java 5
Thread question
Thread query
Difference between passing object to constructor?
JSPWriter difference write vs. print?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:15:36.