vendredi 8 août 2014

Multithreading - différence entre ces deux Java (objets de création) - Stack Overflow


I'm learning about threads in Java, and I've come across these two:


Class Runner implements runnable and is passed onto the constructor in the Thread object



  1. Runner runner = new Runner();
    Thread thread1 = new Thread(runner);


  2. Thread thread1 = new Thread(new Runner());



I've never encountered the second option. It would be great if someone could help a beginner Java programmer out and tell me what the use is for the second variation, and what it is actually called when you create an instance of a class inside the constructor?


Thank you.




There is no difference except after the first one, you have a variable runner that refers to the Runner, and after the second one, you don't. But both of them create a new Runnerobject, and construct a new Thread using that new object.




This is just the ability to use any expression as an object, whether a variable, or constructor.


As an analogy, the first is like "Grow an apple and put it on the table. Then, make a pie from that apple on the table"


The second is akin to "Grow an apple and make a pie out of it". It simply avoid an intermediate variable to hold the apple (in the code, a runnable)


If you needed to do something to the apple other than to pass it, you'd need to (in most cases) store to an intermediate variable. The analogy here is "Grow the apple, put it on the table. Peel the apple. Make a new pie using the apple"




I am giving takeaways first


They both are same and JIT will optimize the first version into second


One main difference though. If you ever want to call some function of runner you can do so at the first version, but you can't at the second one.



I'm learning about threads in Java, and I've come across these two:


Class Runner implements runnable and is passed onto the constructor in the Thread object



  1. Runner runner = new Runner();
    Thread thread1 = new Thread(runner);


  2. Thread thread1 = new Thread(new Runner());



I've never encountered the second option. It would be great if someone could help a beginner Java programmer out and tell me what the use is for the second variation, and what it is actually called when you create an instance of a class inside the constructor?


Thank you.



There is no difference except after the first one, you have a variable runner that refers to the Runner, and after the second one, you don't. But both of them create a new Runnerobject, and construct a new Thread using that new object.



This is just the ability to use any expression as an object, whether a variable, or constructor.


As an analogy, the first is like "Grow an apple and put it on the table. Then, make a pie from that apple on the table"


The second is akin to "Grow an apple and make a pie out of it". It simply avoid an intermediate variable to hold the apple (in the code, a runnable)


If you needed to do something to the apple other than to pass it, you'd need to (in most cases) store to an intermediate variable. The analogy here is "Grow the apple, put it on the table. Peel the apple. Make a new pie using the apple"



I am giving takeaways first


They both are same and JIT will optimize the first version into second


One main difference though. If you ever want to call some function of runner you can do so at the first version, but you can't at the second one.


0 commentaires:

Enregistrer un commentaire