Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Class and Runnable Interface  RSS feed

 
velmurugan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we want to create thread then
we extend Thread class or Implement Runnable interface
Why we have two seperate option to creat a Thread
Is there any special reason behind it?

When we use Thread Class and When we use Runnable Interface

Please anyone help me.
 
Sachin Holla
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One technical cause is that Java does not support multiple inheritance. You cannot extend Thread when your class is already extending another. Another situation is when one who maintains the thread and one who provides the functionality are different � something like a generic thread pool.

One more reason is the coding style itself. People say if you "extend" a class you should be "adding" one or more functionalities to it. Such go for Runnable approach if the need is just to implement run() method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!