• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is difference between Extending Thread and Runnable Interface

 
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone

i am practicing multithreading today and i want understand

What is difference between Extending Thread and Runnable Interface and which one is better.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should know Java can only inherit (extend) one class, while can implement multiple interfaces. Runnable is an interface. So base on this alone, the "implements Runnable" is the preferred choice.

Furthermore, the "extends Thread" creates its own unique instance variables every time, while "implements Runnable" does not.

A more detailed explanation with example here
 
Saloon Keeper
Posts: 15484
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a bit of a special case though, because it was a huge design mistake to make Thread Runnable in the first place. Tasks should be runnable, and a thread does not represent a task. Instead, a thread is what runs a task.

That means that in the past, the best way was to create a Runnable that represents your task, and pass it to a Thread constructor.

Nowadays though, you should not be touching the Thread class at all. Instead, pass your Runnable to ExecutorService.submit().
 
Sudhir Kumar Ojha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great man its work
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic