1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Runnable vs Thread

Discussion in 'Java' started by prashantSum, Nov 21, 2005.

  1. prashantSum

    prashantSum New Member

    Joined:
    Oct 24, 2005
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    hi all,
    I think this is a regular question in interviews, and may be every seems to know the answer, that is which one is better implementing Runnable Interface or extending Thread,..

    and... I heard lot of people saying implementing Runnable interface is better...

    but... I din't understood why implementing Runnable interface is better if so...
    what I think is both are different ways of creating threads..

    some explanation please......
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Its always better to implement a Runnable interface than to extend a Thread because

    1 Using an interface it more flexible than subclassing.
    2 Implementing an interface keeps OO option open and you can use inheritance.

    Thanks
    Shabbir Bhimani
     
  3. prashantSum

    prashantSum New Member

    Joined:
    Oct 24, 2005
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    hi shabbir,
    I din't understood clearly.....

    about 1. why it flexible using Interface .. is this because we can extend some other object?
    about 2. what OO options are option, I think only option open (other options are still available by subclassing also) by using an Interface is mutliple inheritance.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Because In the Runnable version you just create one ThreadedClass object and then created a bunch of Thread objects based on this Runnable object. Where as In "extends Thread" version, you actually have to create a new ThreadedClass object per thread that you want to start.
    Yes You have an option of inheriting the clas from other Class if you wish to as you have not blocked the single inheritance by sub classing it.
     
  5. nicemothi

    nicemothi New Member

    Joined:
    Nov 5, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Hyderabad
    Can any one expalin this with sample code..That could help for better understanding..

    thanks for quicker respose..
     
  6. MohanV

    MohanV New Member

    Joined:
    Apr 28, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    if the thread class you are creating is to be subclass of some other class, it can’t extend from the Thread class. This is because Java does not allow a class to inherit from more than one class. In such a case one can use Runnable interface to implement threads.
     

Share This Page