Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What can possibly happen with a failed Singleton?

823340Dec 17 2010 — edited Dec 20 2010
Hello,
Ive read up on the use of the Singleton Pattern, which goal is to make sure that there is only one instance of the given class.

I got a multithreaded application and Ive found alot of, "bla bla bla", about the problem that can occur when not creating the getInstance method correctly like this:
public static synchronized Singleton getInstance() {
	if (instance == null) {
		instance = new Singleton();
	}
	return instance;
}
But I cant find anything about what problems there is that can occur if you do this wrong and dont use synchronized and there is indeed two instances created instead of one.
Im using this already for my DB layer, but, what can possibly happen if there is two instances instead of one?
Also, as the instance is static, it will never get garbage collected, right? So this can only happen one time at the startup?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 17 2011
Added on Dec 17 2010
9 comments
1,023 views