Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7096

Re: Singleton Pattern

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Singleton Pattern
Date 2011-08-13 20:56 -0400
Organization A noiseless patient Spider
Message-ID <j276gg$pnv$1@dont-email.me> (permalink)
References <3be6e6cf-fa32-4503-9457-b0a1caef8f29@w11g2000vbp.googlegroups.com>

Show all headers | View raw


On 8/13/2011 4:56 PM, vbhavsar@gmail.com wrote:
> People have been coming up with creative solutions to lazily implement
> the singleton pattern in a thread-safe way. We have seen things like
> double-checked locking and creating instance via a single-elemnt enum
> type.
>
> I have thought of yet another way to implement this in a lazy and
> thread-safe way. I haven't seen this proposed anywhere and it seems to
> work unless I am missing something. Here it goes:
>
> public class Singleton {
>
> 	private static Singleton _instance;
> 	private Singleton(){}
>
> 	private synchronized static void createInstance(){
> 		_instance = new Singleton();
> 	}
>
> 	public static Singleton getInstance(){
> 		if (_instance == null){
> 			createInstance();
> 		}
> 		return _instance;
> 	}
> }
>
>
> The synchronized createInstance() method would eliminate the need to
> do double-checked locking and the synchronization would happen only
> when multiple threads call getInstance() before _instance has been
> instantiated.
>
> Anyone see any issues with this?

     Yes.

	T1: if (_instance == null)
	    "Aha! It's null! Let's go make one."

	** context switch **

	T2: if (_instance == null)
	    "Aha! It's null! Let's go make one."

	T2: _instance = createInstance();  // instance #1

	** context switch **

	T1: _instance = createInstance();  // instance #2

... and the two threads go merrily on their way with references
to two different Singleton instances.  With N threads, you could
get as many as N distinct instances.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Singleton Pattern "vbhavsar@gmail.com" <vbhavsar@gmail.com> - 2011-08-13 13:56 -0700
  Re: Singleton Pattern markspace <-@.> - 2011-08-13 14:41 -0700
  Re: Singleton Pattern Patricia Shanahan <pats@acm.org> - 2011-08-13 14:48 -0700
  Re: Singleton Pattern Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-08-13 16:43 -0700
    Re: Singleton Pattern Ian <m4r35n357@gmail.com> - 2011-08-15 15:00 +0100
      Re: Singleton Pattern Lew <lewbloch@gmail.com> - 2011-08-15 07:56 -0700
        Re: Singleton Pattern Patricia Shanahan <pats@acm.org> - 2011-08-15 22:07 -0700
          Re: Singleton Pattern Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-08-16 00:09 -0700
            Re: Singleton Pattern Patricia Shanahan <pats@acm.org> - 2011-08-16 06:15 -0700
              Re: Singleton Pattern Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-08-16 06:57 -0700
                Re: Singleton Pattern Patricia Shanahan <pats@acm.org> - 2011-08-16 09:25 -0700
                Re: Singleton Pattern Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2011-08-16 18:10 -0700
  Re: Singleton Pattern Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-13 20:56 -0400
    Re: Singleton Pattern Lew <lewbloch@gmail.com> - 2011-08-13 21:12 -0700
  Re: Singleton Pattern Rajeev <rajeev.nospam@gmail.com> - 2011-08-14 06:37 -0700

csiph-web