Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Thread vs Runnable better OO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thread vs Runnable better OO" Watch "Thread vs Runnable better OO" New topic
Author

Thread vs Runnable better OO

ZahidM Khan
Greenhorn

Joined: Apr 06, 2012
Posts: 8
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread vs Runnable better OO
 
Similar Threads
Thread --Extend vs implements
Thread Question - Examlab Practice Exam 1
Thread Performance
Doubt in Thread run method.
Instantiating a Thread page no 679