"Implements Runnable" VS "Extends Thread" in Java

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 "purer" way to go.

In practical terms, it means you can implement Runnable and extend from another class as well... and you can also implement Runnable via a lambda expression as of Java 8.

Why implements Runnable is Preferred over extends Thread?

The most common difference is:

When you extend Thread class, you can’t extend any other class which you require. (As you know, Java does not allow inheriting more than one class). When you implement Runnable, you can save a space for your class to extend any other class in future or now.

However, the significant difference is.

When you extends Thread class, each of your thread creates unique object and associate with it. When you implements Runnable, it shares the same object to multiple threads.

Check this:
http://manikandanmv.wordpress.com/tag/extends-thread-vs-implements-runnable/

Why should I use Runnable instead of Thread?

  1. Java doesn't support multiple inheritance, which means you can only extend one Java class, so once you extended Thread class you lost your chance and cannot extend (inherit) another class in Java.

  2. In OOP, extending a class generally means adding new functionality, modifying or improve behaviors. If you are not making any modification on Thread, then use Runnable interface instead.

  3. Implementing Runnable makes your class more flexible (you can implement more than one interface).

Why does 'extends Thread' exist, when 'implements Runnable' is winner in all cases

Because sometimes (almost never, but sometimes) you want to be able to change the basic behaviour of Thread.

That's when you'll need to extend it.

You can change it by overriding a method from the Thread class, you can't do it by implementing one from Runnable.

Implementing Runnable vs. extending Thread

This way you decouple the computation (the what) from the execution (the when and/or the how).

With Runnable or Callable, you can for instance submit many work/computation to an Executor which will take care to schedule the stuffs. Here is an excerpt form ExecutorService:

pool = Executors.newFixedThreadPool(poolSize);
...
pool.execute(new Handler(serverSocket.accept()));
...
class Handler implements Runnable {
...
}

Using Runnable/Callable gives you more flexibility that using Threads directly.

Scenario where extending thread is preferred than implement Runnable?

You should extend a Thread as much as you extend other library classes.

Take a List for example, more specifically ArrayList, you could add extra behaviour on it, like rejecting a value when adding if a certain predicate fails.

Then you can call that an PredicatedArrayList.

It is still a debate whether you want to extend ArrayList here or not, but that debate is not up for this question.

So an example of extending a thread would be a thread that kills itself after a specific amount of time. Then you would have SuicidingThread extends Thread, which could have a constructor taking the time.

This even fortifies the argument that you should put your actual tasks in a Runnable.

Like Runnable somethingRunnable = new SomeClass();, where SomeClass implements Runnable.

Now you can do either:

  • Thread someThread = new Thread(somethingRunnable);
  • Thread someThread = new SuicidingThread(somethingRunnable, 5, TimeUnit.DAYS);

So this would be an usecase for extending thread.

threads runnable vs extends thread

I think you've confused yourself with your naming conventions. I assume you were trying to do this:

public static void main(String args[]){  
Table obj = new Table();//only one object
Thread thread1 = new Thread(new t1(obj)); // t1 is the Runnable class
MyThread2 thread2 = new MyThread2(obj);
thread1.start();
thread2.start();
}

Extends Thread and implement runnable simultaneously

You should almost never do that. The type Thread already implements Runnable.

The only reason to do this is if you want to be explicit in your source code.

both type of thread creation

There is only one way to create a thread: creating a Thread instance and invoking its start() method. Runnable is just an interface.



Related Topics



Leave a reply



Submit