Quantcast
Channel: "implements Runnable" vs "extends Thread" in Java - Stack Overflow
Browsing index pages (45 articles)
↧

Answer by Yogesh Sharma for "implements Runnable" vs "extends Thread" in Java

If you want to separate your concerns like, if you want to create Tasks and it does not matter to you who is the worker working on your task then use Runnable and define your work as a task. Just like...

View Article


Image may be NSFW.
Clik here to view.

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

Lambda expression with functional programming in java

View Article


"implements Runnable" vs "extends Thread" in Java

From what time I've spent with threads in Java, I've found these two ways to write threads: With implements Runnable: public class MyRunnable implements Runnable { public void run() { //Code } }...

View Article

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

I'm not an expert, but I can think of one reason to implement Runnable instead of extend Thread: Java only supports single inheritance, so you can only extend one class. Edit: This originally said...

View Article

Answer by Jon Skeet for "implements Runnable" vs "extends Thread" in Java

Yes: implements Runnable is the preferred way to do it, IMO. You're not really specialising the thread's behaviour. You're just giving it something to run. That means composition is the philosophically...

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

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 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 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 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 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 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 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 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 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 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 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 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 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 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
Browsing index pages (45 articles)


Latest Images