The moose likes Threads and Synchronization and the fly likes Difference between Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Difference between" Watch "Difference between" New topic
Author

Difference between

Anu Charitha
Greenhorn

Joined: Apr 07, 2009
Posts: 4




Thanks for help in advance.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3133
Hi, Anu, and welcome to the Ranch!

In the future, please UseAMeaningfulSubjectLine(⇐click).

As for your question, you can see for yourself what the difference is. In the first case, you have two MyRunnable objects and in the second case you only have one.
Caleb Kemper
Greenhorn

Joined: Feb 08, 2012
Posts: 14

Also, the first one declares two anonymous objects, while the second utilizes one that is referenced by a pointer variable, 'r'.
Use the first method if you're not going to be using those objects again. Use the second method if you are.

Example code (reading in an image):

Note that, because I only need to create a File object for the sole purpose of reading in the image, I use an anonymous object.
Otherwise, if the File were to have some future use, I would have created the File before, set it equal to a variable, and then used it in the read() method.

If you're not sure whether you're going to use an object again, it's best to simply assign it a pointer variable and play it safe.


Caleb

 
 
subject: Difference between
 
Threads others viewed
Threads
Regarding Threads
Thread Question about sleep method
How to access variable from run() method
Anonymous Issue
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture