I learned a fun bit of java trivia the other day, that I thought I'd share. Let's say you have a lazy-loaded singleton design including the following code:
...
public static MySingletonClass getInstance() {
if (instance == null) {
instance = new MySingletonClass();
}
return instance;
}
...Of course, in a threaded application, you'll want to make the setting synchronized, so you can be sure that two threads calling getInstance() simultaneously for the first time don't create it twice. So all you need to do is synchronize it, right?