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.

4
  • 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 Commented Sep 18, 2014 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. Commented Sep 18, 2014 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. Commented Sep 19, 2014 at 6: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. Commented Sep 19, 2014 at 13:09

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.