Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread vs Runnable better OO

 
ZahidM Khan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can define and instantiate a thread in one of two ways:
■ Extend the java.lang.Thread class.
■ Implement the Runnable interface.

In both cases we have to override run() method.

How come when we extend class we combine both the thread and the job (the code in the run()method) into one class, and when we implement Runnable we've split it into two classes—the Thread class for the thread-specific code and your Runnable implementation class for your job-that-should-be-run-by-a-thread code.?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Thread class should never have been declared to implement Runnable. It is a design flaw that we can extend Thread instead of implementing Runnable. Thread's job is to execute a task in its own thread of execution. Runnable's job is to be that task.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic