0

When I should use

class MyThread extends Thread implements Runnable{
//any statement.
}

And what is the significance of using both type of thread creation at a single time.

JavaBeigner
  • 608
  • 9
  • 28
  • Every Thread is a Runnable.You are basically overriding run method() present in Runnable interface.Use @Override annotation in your MyThread class for your run method – Kumar Abhinav Sep 18 '14 at 10:04
  • 1
    What do you mean, "both type of thread creation?" Your example does not show _any_ type of thread creation. As @SotiriosDelimanolis said in his answer, `implements Runnable` is redundant: Thread implements runnable, and MyThread extends Thread, therefore MyThread implements Runnable whether you say so or not. – Solomon Slow Sep 18 '14 at 12:57
  • @jameslarge I mean there are basically two way to create thread in java, by extending Thread class or implemeneting Runnable interface. so my question is that what special objective if it is, we can achieve by doing both way at one time. –  Sep 19 '14 at 06:40
  • OK, but you can't extend the Thread class without creating a Runnable because Thread _is_ Runnable. The real choice is, do you extend the Thread class, or do you create a base Thread instance and give it a Runnable _delegate_. The software engineering community has pretty much come to realize that delegation is a better way to design software. When you write code where a Foo _has_ a Bar, it tends to be easier to test, easier to understand, and easier to maintain then if you wrote it so that a Foo _is_ a Bar. – Solomon Slow Sep 19 '14 at 13:09

1 Answers1

4

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724