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. If your class is extending the Thread class then it becomes a single thread which inherits the properties Thread class , so it'll be heavy.
When extending Thread class each of the threads creates unique object and associate with it, but when implementing Runnable , it shares the same object to multiple Threads. If your class is Implementing the Runnable interface then you only override the run. So this instance creates a separate Thread and every individual Thread runs separately but not as a single heavy Thread in your program.
Another thing, Since Java does not support multiple inheritance , if you implement the Runnable you'll avoid problems of multiple extending, so if you implement Runnable interface you can extend any class that you are required other than Thread class. A class may only have one superclass, but may implement any number of interfaces. By extending Thread you give up the opportunity to subclass anything else.
Java only allows single inheritance, which means that if you inherit from Thread you won't be able to inherit from any other class. Implementing the Runnable interface doesn't have this limitation, since your class is allowed to implement any number of interfaces. Anyway one reason i know that as we can achieve the same functionality using Runnable, so we should go for it because if we will implement Runnable then we can extend other class which is not possible if we will extend Thread class.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. Why "implements Runnable" is Preferred over "extends Thread"? This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class. Since: JDK1. Method Detail run void run When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread. The general contract of the method run is that it may take any action whatsoever.
See Also: Thread. Guest Posts. Secure Hash Algorithms. Best Way to Learn Java. How to Start New Blog. Skip to content. Runnable interface Java program to create thread by implementing Runnable interface. Thread class Java program to create thread by extending Thread class. Difference between Runnable vs Thread There has been a good amount of debate on which is better way.
Implementing Runnable is the preferred way to do it. That means composition is the better way to go. Java only supports single inheritance, so you can only extend one class. Instantiating an interface gives a cleaner separation between your code and the implementation of threads. Implementing Runnable makes your class more flexible.
You can run it in a thread, or pass it to some kind of executor service, or just pass it around as a task within a single threaded application. Happy Learning!! Was this post helpful? Let us know if you liked the post. Difference between sleep and wait in Java. Java executor framework tutorial and best practices. Thread creating using Runable is bettter in terms of lamda expression and Functional interface Reply.
Hello Sowmya rajan, As per my understanding, Thread is a curse in java. Well Explained.
0コメント