BIRT 3.7
Written by: Michael Williams
Featured Refcardz: Top Refcardz:
  1. Scrum
  2. Apache Maven 2
  3. Essential MySQL
  4. Node.js
  5. Groovy
  1. jQuery Selectors
  2. Ajax
  3. Java
  4. Spring Config.
  5. Java Concurrency

Link Details

Link 456089 thumbnail
User 225256 avatar

By mswatcher
via codeproject.com
Published: Jul 30 2010 / 02:35

Everything there is to know about Singletons and thread safety.
  • 9
  • 1
  • 1843
  • 0

Comments

Add your comment
User 447031 avatar

iirekm replied ago:

1 votes Vote down Vote up Reply

One thing you probably forgot: add 'volatile' keyword to the variable that holds singleton. Otherwise some compilers could cache the value in a CPU registry and you may get unpredictable results.
The same 'double-checking' pattern works also in Java (since 1.5).

The problem with singleton is that if you have lots of singleton objects, this leads to lots of ugly boilerplate code. Better use a dependency injection framework, like Spring in Java.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.