Results 1 to 6 of 6

Thread: [RESOLVED] new Runnable(){...}.run()

  1. #1

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Resolved [RESOLVED] new Runnable(){...}.run()

    Code 1:
    Code:
    new Runnable(){
                public void run() {
                    assert(false);
                }
            }.run();

    Code 2:
    Code:
    new  Thread(new Runnable(){
                public void run() {
                    assert(false);
                }
            }).start();
    Apart from the obvious differences, what are their differences and what is the advantage or disadvantage of each?

    Need Advice.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: new Runnable(){...}.run()

    The 1st use of the Runnable interface is meaningless, because you're creating an instance then calling it's only method leaving a memory load for the garbage collector.

    The 2nd statement is a correct use of the Runnable interface. Creating a Runnable instance to run on a different thread
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: new Runnable(){...}.run()

    Quote Originally Posted by ComputerJy
    The 1st use of the Runnable interface is meaningless, because you're creating an instance then calling it's only method leaving a memory load for the garbage collector.
    I don't understand that bit about the memory load and I have never seen code done this way prior to messing around with a source code. So it got me wondering that there might be some advantages to doing execution this way.

    Could you elaborate on the memory load thing? And in what circumstances would the first code be considered?

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: new Runnable(){...}.run()

    Quote Originally Posted by oceanebelle
    in what circumstances would the first code be considered?
    Never
    Quote Originally Posted by oceanebelle
    Could you elaborate on the memory load thing?
    Every time your code gets executed, A new Runnable Object will be instantiated, then the method run will be invoked. which will lead to:
    Unnecessary instructions and An object in memory with no references to it awaiting garbage collection...
    So there are no advantages
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: new Runnable(){...}.run()

    I see thanks for the explanation!

    I'll bear that in mind. However, I'll have to live with this code I guess.

  6. #6

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: [RESOLVED] new Runnable(){...}.run()

    And again, I'll have to spread the love before I can rate you ComputerJy. :sad:

Posting Permissions

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



Click Here to Expand Forum to Full Width