Quantcast
Channel: "implements Runnable" vs "extends Thread" in Java - Stack Overflow
Browsing all 45 articles
Browse latest View live
↧

Answer by dharam for "implements Runnable" vs "extends Thread" in Java

Can we re-visit the basic reason we wanted our class to behave as a Thread? There is no reason at all, we just wanted to execute a task, most likely in an asynchronous mode, which precisely means that...

View Article


Answer by user2771655 for "implements Runnable" vs "extends Thread" in Java

if you use runnable you can save the space to extend to any of your other class.

View Article


Answer by Nikhil A A for "implements Runnable" vs "extends Thread" in Java

One reason you'd want to implement an interface rather than extend a base class is that you are already extending some other class. You can only extend one class, but you can implement any number of...

View Article

Image may be NSFW.
Clik here to view.

Answer by Nidhish Krishnan for "implements Runnable" vs "extends Thread" in Java

If you want to implements or extends any other class then Runnable interface is most preferable, otherwise, if you do not want any other class to extend or implement then Thread class is preferable....

View Article

Answer by didierc for "implements Runnable" vs "extends Thread" in Java

That's the S of SOLID: Single responsibility. A thread embodies the running context (as in execution context: stack frame, thread id, etc.) of the asynchronous execution of a piece of code. That piece...

View Article


Answer by Rupesh Yadav for "implements Runnable" vs "extends Thread" in Java

Well so many good Answers, I want to add more on this. This will help to understand Extending v/s Implementing Thread.Extends binds two class files very closely and can cause some pretty hard to deal...

View Article

Answer by developer110 for "implements Runnable" vs "extends Thread" in Java

Runnable is an interface, while Thread is a class which implements this interface. From a design point of view, there should be a clean separation between how a task is defined and between how it is...

View Article

Answer by AHugoH for "implements Runnable" vs "extends Thread" in Java

This maybe isn't an answer but anyway; there is one more way to create threads: Thread t = new Thread() { public void run() { // Code here } }

View Article


Answer by AntonyM for "implements Runnable" vs "extends Thread" in Java

Everyone here seems to think that implementing Runnable is the way to go and I don't really disagree with them but there is also a case for extending Thread in my opinion, in fact you have sort of...

View Article


Answer by Himanshu Mohta for "implements Runnable" vs "extends Thread" in Java

Java does not support multiple inheritence so if you extends Thread class then no other class will be extended. For Example: If you create an applet then it must extends Applet class so here the only...

View Article

Answer by Tarvaris Jackson for "implements Runnable" vs "extends Thread" in Java

I find it is most useful to use Runnable for all the reasons mentioned, but sometimes I like to extend Thread so I can create my own thread stopping method and call it directly on the thread I have...

View Article

Answer by Manoj Kumar for "implements Runnable" vs "extends Thread" in Java

Yes, If you call ThreadA call , then not need to call the start method and run method is call after call the ThreadA class only. But If use the ThreadB call then need to necessary the start thread for...

View Article

Answer by Bart van Heukelom for "implements Runnable" vs "extends Thread" in...

I would say there is a third way: public class Something { public void justAnotherMethod() { ... } } new Thread(new Runnable() { public void run() { instanceOfSomething.justAnotherMethod(); }...

View Article


Answer by n13 for "implements Runnable" vs "extends Thread" in Java

Runnable because: Leaves more flexibility for the Runnable implementation to extend another class Separates the code from execution Allows you to run your runnable from a Thread Pool, the event thread,...

View Article

Answer by panzerschreck for "implements Runnable" vs "extends Thread" in Java

Moral of the story: Inherit only if you want to override some behavior. Or rather it should be read as: Inherit less, interface more.

View Article


Answer by Govula Srinivas for "implements Runnable" vs "extends Thread" in Java

Separating the Thread class from the Runnable implementation also avoids potential synchronization problems between the thread and the run() method. A separate Runnable generally gives greater...

View Article

Answer by Herms for "implements Runnable" vs "extends Thread" in Java

One thing that I'm surprised hasn't been mentioned yet is that implementing Runnable makes your class more flexible. If you extend thread then the action you're doing is always going to be in a thread....

View Article


Answer by Fabian Steeg for "implements Runnable" vs "extends Thread" in Java

You should implement Runnable, but if you are running on Java 5 or higher, you should not start it with new Thread but use an ExecutorService instead. For details see: How to implement simple threading...

View Article

Answer by Bob Cross for "implements Runnable" vs "extends Thread" in Java

tl;dr: implements Runnable is better. However, the caveat is important In general, I would recommend using something like Runnable rather than Thread because it allows you to keep your work only...

View Article

Answer by starblue for "implements Runnable" vs "extends Thread" in Java

Instantiating an interface gives a cleaner separation between your code and the implementation of threads, so I'd prefer to implement Runnable in this case.

View Article
Browsing all 45 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>