Results 1 to 5 of 5
  1. #1
    viki1719 is offline Member
    Join Date
    May 2013
    Posts
    8
    Rep Power
    0

    Default advantage of Runnable over Thread

    Hi All,
    here i am going to start a thread which is one of the most common question that is we have to way to create a thread in java.
    1-by extending Thread class in our class
    2-by implementing Runnable interface into our class
    NOTE: implementing Runnable is always recommended to use.

    let me explain what i understand form my own way is that....
    1-generally, we extends a class into another class only when we want to enhanced its behavior by overriding the methods then we should extends the class ,but in the case of Thread class our main aim to achieve run() method inside our class and we do not override Thread class any method in our class instead of run(), so here Runnable would be the best choice because it contains only one method..
    2-if we extends Thread class in our class then by inheritance concept we won't be able to extend anymore class in our class as java does't support for multiple inheritance so here by doing this we are missing the inheritance benefit if once we extended Thread class instead of this if we implement Runnable interface then we can extends any class according to our requirement.

    instead of above any other real time benefit of Runnable over Thread ,please feel free to reply with your answer .
    your reply would be highly appreciated.

    feel free to correct me if i have stated wrong above.
    thanks for your time to read this thread.

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: advantage of Runnable over Thread

    Composition over inheritance. Using a Runnable in other contexts, such as through Executors

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,927
    Rep Power
    4

    Default Re: advantage of Runnable over Thread

    You're correct from the point of not supporting multiple inheritance. My recommendation would be that you should implement Runnable as opposed to extending Thread. In fact, as a general rule, I would say that if you can accomplish your goal without subclassing then do so. Inheritance tends to break encapsulation and can cause problems if the super classes weren't carefully designed to support inheritance. I tend to do the following:

    Java Code:
     new Thread("MyThread") {
             public void run() {
                // stuff to run in thread
                System.out.println("Hello from " + Thread.currentThread().getName());
             }
          }.start();
       }

    If I want to invoke different methods within different threads, I usually forgo implementing the interface as part of the enclosing class and instead use anonymous thread classes (since Thread implements Runnable()). I just override run() as above. If I need to use some of the thread methods I will save an instance of it. You can also pass in instance of Runnable() as an argument to Thread.

    If you want to invoke the same thread over and over again, you could used an inner class which implements Runnable. It really depends on what you want to do.

    I am also using the java.util.concurrent utilities more often which makes things easier.

    Check out the thread section in the Java Tutorials (see my signature link). It covers the latest and greatest on thread support added in Java 1.7.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    131
    Rep Power
    0

    Default Re: advantage of Runnable over Thread

    Why not thread factories so you can start a thread few times or stop it?

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,927
    Rep Power
    4

    Default Re: advantage of Runnable over Thread

    I agree. Those are provided by java.util.concurrent which I mentioned above. However, I find it educational to also roll your own. Understanding the use of Thread, Runnable, synchronization, and the wait and notify methods is useful to know.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 11:35 AM
  2. advantage of using thread
    By simorgh in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-18-2010, 05:53 AM
  3. Thread RUNNABLE or WAITING
    By Pushkar in forum Threads and Synchronization
    Replies: 10
    Last Post: 01-14-2010, 01:36 AM
  4. runnable interface in thread
    By ADARSH in forum New To Java
    Replies: 1
    Last Post: 11-01-2009, 08:08 PM
  5. Creating a Thread using the Runnable interface
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •