bsarsgard's blog
Double-Checked Locking in Synchronized Java Code
Submitted by bsarsgard on Thu, 05/01/2008 - 3:46pm.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?
SingleInstanceService and Alternatives with Java Web Start
Submitted by bsarsgard on Wed, 04/30/2008 - 10:53pm.Matt and I have been experimenting with part of the JNLP API called SingleInstanceService. Basically, it's a sort of super-static singleton class that's shared between all running instances of the application. This is really cool in theory, as it allows you do do things like count the number of running instances, and allow only one. There's some great code implementation examples, as well.


