July 2001
Beginner to intermediate
368 pages
6h 52m
English
This pattern only applies to multithreaded applications. If you are not involved with multithreaded applications you might want to skip this section. This section assumes that you have a basic understanding of multithreaded issues, including synchronization.
A problem with the Singleton pattern may arise in multithreaded applications.
Suppose two calls to getInstance() are made at exactly the same time. This can be very bad. Consider what can happen in this case:
The first thread checks to see if the instance exists. It does not, so it goes into the part of the code that will create the first instance.
However, before it has done that, suppose a second thread also looks to see if the instance member ...