Difference between two implementing the Thread

Discussion in 'Java' started by Proton Projects - Moin, Feb 24, 2007.

  1. Hi all,

    There are two way of Thread Implementation

    1. Extending a Thread Class
    2. Implementing the Runnable Interface

    My Question are
    1. What is the actual difference between the implementations?
    2. Why Java provided the two way implementation

    I got sick of hearing the regular answer saying that if we extend the
    Thread Class...we cant extend any other class....I find this is a
    silly justification...

    I would like to know the root reason behind the two implementation

    Kindly Answer
    Moin
    Proton Projects - Moin, Feb 24, 2007
    #1
    1. Advertising

  2. Proton Projects - Moin

    Guest

    On 24 Feb., 09:13, "Proton Projects - Moin" <>
    wrote:
    > I got sick of hearing the regular answer saying that if we extend the
    > Thread Class...we cant extend any other class....I find this is a
    > silly justification...


    So you are not prepared to listen to arguments which overwhelm your
    pea size brain. Than go ahead and build your junk software the way you
    prefer.
    , Feb 24, 2007
    #2
    1. Advertising

  3. On Feb 24, 1:48 pm, wrote:
    > On 24 Feb., 09:13, "Proton Projects - Moin" <>
    > wrote:
    >
    > > I got sick of hearing the regular answer saying that if we extend the
    > > Thread Class...we cant extend any other class....I find this is a
    > > silly justification...

    >
    > So you are not prepared to listen to arguments which overwhelm your
    > pea size brain. Than go ahead and build your junk software the way you
    > prefer.


    I dont want the answer who doesnt understand the situation of the
    Questioner....I asked most of the people offline..but i got this
    answer " if we extend the Thread Class...we cant extend any other
    class...." i am not satisfied with the answer...for this reason...Java
    has provided the functionality of two way implementation...

    If you are about the answer the same reason....i dont need your
    answer....

    Regards
    Moin
    Proton Projects - Moin, Feb 24, 2007
    #3
  4. Proton Projects - Moin

    Daniel Dyer Guest

    On Sat, 24 Feb 2007 09:04:44 -0000, Proton Projects - Moin
    <> wrote:

    > On Feb 24, 1:48 pm, wrote:
    >> On 24 Feb., 09:13, "Proton Projects - Moin" <>
    >> wrote:
    >>
    >> > I got sick of hearing the regular answer saying that if we extend the
    >> > Thread Class...we cant extend any other class....I find this is a
    >> > silly justification...

    >>
    >> So you are not prepared to listen to arguments which overwhelm your
    >> pea size brain. Than go ahead and build your junk software the way you
    >> prefer.

    >
    > I dont want the answer who doesnt understand the situation of the
    > Questioner....I asked most of the people offline..but i got this
    > answer " if we extend the Thread Class...we cant extend any other
    > class...." i am not satisfied with the answer...for this reason...Java
    > has provided the functionality of two way implementation...


    The Runnable interface is effectively Java's substitute for function
    pointers. All the Runnable interface says about a class is that here is
    some code that can be executed. It doesn't say anything about where that
    code should be run or when. You can run it on a new thread or you can run
    it on the current thread - it's up to you.

    If, instead of implementing Runnable you extend Thread, you are tying the
    logic to a single particular concrete thread. You can only start a thread
    once. With a Runnable, you can pass it to several threads and run it as
    many times as you like, even simultaneously if it's written in a way that
    makes it safe to do so.

    Another point that is often made when discussing this point is that by
    sub-classing Thread you are effectively saying this class is a new *type*
    of thread, when in reality it is not, it is just a normal thread that has
    an unnecessary coupling between what it does and how it does it. So the
    reason not to sub-class thread is not so much because it prevents
    extending another class, but because conceptually it rarely makes sense
    for your Runnable to extend *any* base class.

    Dan.

    --
    Daniel Dyer
    https://watchmaker.dev.java.net - Evolutionary Algorithm Framework for Java
    Daniel Dyer, Feb 24, 2007
    #4
  5. Proton Projects - Moin

    Eric Sosman Guest

    Proton Projects - Moin wrote:
    > Hi all,
    >
    > There are two way of Thread Implementation
    >
    > 1. Extending a Thread Class
    > 2. Implementing the Runnable Interface


    The second is not a "way of Thread Implementation" because
    it does not implement a Thread. You can write as many classes
    implementing Runnable as you please, and still have a program
    that is only single-threaded (overtly, at least). To launch
    additional threads, you must create Thread objects and call
    their start() methods.

    > My Question are
    > 1. What is the actual difference between the implementations?


    A Thread runs in its own thread of execution. Runnable
    is just an interface, and does nothing special at all.

    > 2. Why Java provided the two way implementation


    Suppose it provided Runnable but not Thread. Then there
    would be no way to create a multi-threaded program.

    Suppose it provided Thread but not Runnable. Then the
    only way to get "your code" to run in a separate thread would
    be for you to extend the Thread class. This leads to two
    further strategies: You can put "your code" directly in your
    SubThread class, or you can put it in your Thing class and
    have SubThread call a Thing method. The first approach limits
    your ability to build class hierarchies, and the second amounts
    to reinventing Runnable for yourself. Instead of having every
    Java programmer in the world reinventing his own Runnable, Java
    provides one ready-made. You are not forced to use it.

    > I got sick of hearing the regular answer saying that if we extend the
    > Thread Class...we cant extend any other class....I find this is a
    > silly justification...


    I'm sorry you are sick. Some people find nausea entertaining,
    but to the nauseous it is not the least bit funny. Please stand
    downwind until you feel better.

    > I would like to know the root reason behind the two implementation


    The root cause is that the early Universe contained slightly
    different quantities of matter and antimatter. Had that not been
    the case, Java would not be as it is.

    --
    Eric Sosman
    lid
    Eric Sosman, Feb 24, 2007
    #5
  6. Proton Projects - Moin

    Daniel Pitts Guest

    On Feb 24, 12:13 am, "Proton Projects - Moin"
    <> wrote:
    > Hi all,
    >
    > There are two way of Thread Implementation
    >
    > 1. Extending a Thread Class
    > 2. Implementing the Runnable Interface
    >
    > My Question are
    > 1. What is the actual difference between the implementations?
    > 2. Why Java provided the two way implementation
    >
    > I got sick of hearing the regular answer saying that if we extend the
    > Thread Class...we cant extend any other class....I find this is a
    > silly justification...
    >
    > I would like to know the root reason behind the two implementation
    >
    > Kindly Answer
    > Moin


    Here is the difference:

    creating a Thread object, and calling .start() on it will cause a new
    thread of execution to start (you know this already...)

    Thread.run() is the *always* what is run in the new thread. The
    implementation of Thread.run() is to call run() on the Runnable object
    passed to it in the constructor.

    When yo extend Thread, you override the run() method, so the default
    behavior doesn't happen, but your behavior does.

    HTH,
    Daniel.
    Daniel Pitts, Feb 24, 2007
    #6
  7. Proton Projects - Moin

    Chris Uppal Guest

    Proton Projects - Moin wrote:

    > 2. Why Java provided the two way implementation


    Why does Java allow you to specify what a thread does using the Runnable
    interface ? You've already been told several times.

    Why does Java /also/ allow you to do it by extending class Thread ? Because
    there is no reason to forbid it, and because for some (fairly rare)
    applications it is desirable to attach extra information or behaviour to the
    thread that executes some action.

    -- chris
    Chris Uppal, Feb 24, 2007
    #7
  8. Proton Projects - Moin wrote:
    > Hi all,
    >
    > There are two way of Thread Implementation
    >
    > 1. Extending a Thread Class
    > 2. Implementing the Runnable Interface
    >
    > My Question are
    > 1. What is the actual difference between the implementations?
    > 2. Why Java provided the two way implementation
    >


    In a sense, you're asking the question "Why can I extend the functionality
    of a class either by subclassing or by aggregation and delegation?" I don't
    know an answer better than "Because Java supports both."
    Mike Schilling, Feb 25, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,051
  2. Gonzalo Moreno
    Replies:
    2
    Views:
    12,381
  3. Jack Dowson
    Replies:
    3
    Views:
    263
    Esmond Pitt
    May 7, 2007
  4. Replies:
    5
    Views:
    2,230
    Dominick Baier [DevelopMentor]
    Nov 8, 2005
  5. PerlFAQ Server
    Replies:
    0
    Views:
    257
    PerlFAQ Server
    Feb 2, 2011
Loading...

Share This Page