-1

Could you please give a short explanation of whether there is a significant difference between the following thread implementations:

// Method 1
Thread aThread = new Thread()
{
    @Override
    public void run()
    {
        // do some work
    }
};

aThread.start();

// Method 2      
Thread bThread = new Thread(new Runnable()
{
    @Override
    public void run()
    {
        // do some work
    }
});

bThread.start();

I tried to find similar questions in stackoverflow, but couldn't succeed. Sorry, if it is already discussed before.

8

1 Answer 1

1

As per the Javadoc:

https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#run--

the default run() method of Thread executes the run() method of the Runnable it was created with, if it exists. Otherwise it does nothing. What this means is that creating a Thread with a Runnable does the same thing as overriding Thread's run(), except it uses slightly more stack for the additional function call.

So no. No significant difference.

7
  • If you are going to say, As per the Javadoc, be sure to actually include a link. Jul 21, 2015 at 14:07
  • @JaredBurrows Done. Thank you for the suggestion. Jul 21, 2015 at 14:10
  • I did not down vote you, but this is a duplicate question anyways. Jul 21, 2015 at 14:12
  • It doesn't matter. It was a good suggestion anyway. Jul 21, 2015 at 14:12
  • Tripp Kinetics, thank you for your answer, I upvoted it. I need to search a bit in google to read about what you wrote regarding stack. Jul 21, 2015 at 14:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.