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

best way for implementing thread in java

Discussion in 'Java' started by khushboo652, Jan 11, 2012.

  1. khushboo652

    khushboo652 Banned

    Joined:
    Jan 9, 2012
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    pls give me the answer which is the best way to implement thread in java runnable interface or inheritance and why?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,312
    Likes Received:
    371
    Trophy Points:
    83
    What all ways you have tried and looking for best amont them?
     
  3. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
  4. dagdu

    dagdu New Member

    Joined:
    Feb 8, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    By implementing runnable interface is best way to implement.By implementing we can reuse that thread and we can extends any other useful class.
     
  5. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    4
    Trophy Points:
    0
  6. RRT2010

    RRT2010 New Member

    Joined:
    Apr 7, 2011
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    India
    Also you can see if you require to Synchronize the threads. This may be needed if you have many threads running in parallel to avoid concurrency.
     
  7. ifeeleducation

    ifeeleducation New Member

    Joined:
    Mar 2, 2016
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    CEO
    Location:
    Mumbai
    Home Page:
    Use Class is the best way for implementing thread in java
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,312
    Likes Received:
    371
    Trophy Points:
    83
    On what basis you say it is the best?
     
  9. theglobalassociates

    theglobalassociates New Member

    Joined:
    Apr 5, 2016
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    1.Extending Thread Class
    2.Implementing Runnable Class
     
  10. Jeff Ronald

    Jeff Ronald Member

    Joined:
    Dec 9, 2016
    Messages:
    58
    Likes Received:
    0
    Trophy Points:
    6
    Gender:
    Male
    Home Page:
    There are two ways:
    1.Extending thread class
    2.Using Runnable Interface.
     
  11. persysweb

    persysweb Member

    Joined:
    Aug 1, 2017
    Messages:
    67
    Likes Received:
    11
    Trophy Points:
    8
    Location:
    India
    Home Page:
    The best way to implement thread in java is by implementing Runnable Interface.
    As java does not support multiple inheritance,to remove the ambiguity problem multiple inheritance is not supported by java.
    eg. class A extends B,C this cannot be achieved in java .

    But Multiple inheritance can be achieved in java by using Interface.
    eg.class A extends B implements Runnable.
    So by using Runnable Interface we are saving one class for Extend.
     
  12. priya456

    priya456 New Member

    Joined:
    Jul 7, 2017
    Messages:
    26
    Likes Received:
    2
    Trophy Points:
    3
    Gender:
    Female
    There are two method for creating thread

    1) Extending Thread Class

    2) Implementing Runnable Interface

    Why implements runnable is preferred over extends thread?

    When you extend Thread class, you can’t extend any other class which you require . When you implement Runnable, you can save a space for your class to extend any other class in future or now.
     

Share This Page