Results 1 to 12 of 12
  1. #1
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    396
    Rep Power
    6

    Default Which method is best for creating Threads?

    I know that there are two ways to create threads.
    First one is extends Thread class.
    Second one is Implements Runnable interface.

    Which method is best for Thread application and Why?

    Share your thoughts with me...

    Thanking You.
    Mak
    (Living @ Virtual World)

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    As far as my knowledgee is concerned Thread using Runnable is the best one.
    Because u can extend other class.
    Example : Applets
    Ramya:cool:

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    396
    Rep Power
    6

    Default

    Thanks Ramya for sharing your thoughts with me.


    But my friend told that whenever we use divide and conquer in our application,Extends Thread class is more comfortable than previous one.

    Do you have any idea about it?Are you indian?
    Mak
    (Living @ Virtual World)

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Neither is intrinsically "better"-- which to use largely depends on what seems a more logical design for your program.

    In principle, a Runnable represents what gets run, while a Thread represents how it gets run: the Thread object is where you can set things like name and thread priority, and is where you actually start the task running.

    There are also other ways of running a Runnable without explicitly wrapping a thread around it: for example, the Java 5 concurrency utilities such as ThreadPoolExecutor, or SwingUtilities.invokeLater().

    However, as a "shortcut", you can override the run() method on Thread. This is convenient where (a) your runnable class doesn't need to override anything else, and (b) it makes sense for you to keep the "what" code next to/coupled with the "how" code. Whether (a) and (b) are true really depends on your particular program.

    Some more info I've written: constructing Threads and Runnables.
    Last edited by neilcoffey; 05-06-2009 at 12:37 PM. Reason: added extra examples/refs

  5. #5
    Mrs. Deswal's Avatar
    Mrs. Deswal is offline Member
    Join Date
    Apr 2009
    Location
    Gurgaon,India
    Posts
    24
    Rep Power
    0

    Default

    Hi,

    I am also in favour of implementing Runnable interface instead of extending Thread class.
    Sharing knowledge means gaining more knowledge.:)

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    While I agree with what Neil said, I think that the focus should typically be one what is running, not how. The nice thing about implementing Runnable is that the object does not *have* to be run on a thread. You gain flexibility without losing anything.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    I thought I read in the api not to extend threads, and finally found that passage:
    In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.
    from java.lang.Runnable
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    The JavaDoc has spoken. Word. ;-)

    In this case, AB is correct in quoting the API. I just hear that at work all the time, generally about things where what Sun says really doesn't have anything to do with the problem at hand...

  9. #9
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Implementing runnable is 'better' in the sense of flexibility, but quite often, you don't care about that flexibility and just want 1 simple thing done in a different thread. In a case like that, extending Thread via an anonymous class often provides the most concise and maintainable code.

    It all depends on what is meant by 'better'.

  10. #10
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Only extend the Thread class if you want to extend the function ability of the Thread... hence the name, extend...
    I die a little on the inside...
    Every time I get shot.

  11. #11
    jSarK0Y is offline Member
    Join Date
    Apr 2008
    Posts
    21
    Rep Power
    0

    Default

    it's simply: Runnable are good for visual modules of app -- Thread are good for calculation's tasks.

  12. #12
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    396
    Rep Power
    6

    Default

    Thanks to all those shared your thoughts with this post..

    At last,I would like to say here some thing about this thread is that it is decided that Using Runnable interface is more comfortable rather than using Thread class for Java Programming.

    Thanks a lot Once again.
    Mak
    (Living @ Virtual World)

Similar Threads

  1. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 01:21 PM
  2. multiple threads access the same method.
    By bhanu in forum New To Java
    Replies: 3
    Last Post: 02-16-2009, 06:54 AM
  3. Creating a GUI - which method
    By matpj in forum New To Java
    Replies: 3
    Last Post: 01-15-2009, 04:40 PM
  4. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 05:32 PM
  5. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 06:15 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
  •