Java

Johns Hopkins Medicine's Brady Urological Institute

Johns Hopkins Medicine's Brady Urological Institute

Johns Hopkins Medicine's Brady Urological Institute

Challenge: The Johns Hopkins Medicine's Brady Urological...

View All Case Studies

LJS & Associates, Inc.

LJS Database built by VMT

LJS & Associates, Inc.

Challenge: LJS has a 96% market-share in residential real-estate...

View All Case Studies

Double-Checked Locking in Synchronized Java Code

in

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?

Syndicate content
Powered by Drupal, an open source content management system