Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7094
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.palinacquisition!news.posted.palinacquisition.POSTED!not-for-mail |
|---|---|
| NNTP-Posting-Date | Sat, 13 Aug 2011 18:43:16 -0500 |
| Date | Sat, 13 Aug 2011 16:43:15 -0700 |
| From | Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> |
| User-Agent | Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 |
| MIME-Version | 1.0 |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Singleton Pattern |
| References | <3be6e6cf-fa32-4503-9457-b0a1caef8f29@w11g2000vbp.googlegroups.com> |
| In-Reply-To | <3be6e6cf-fa32-4503-9457-b0a1caef8f29@w11g2000vbp.googlegroups.com> |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Message-ID | <vt-dnf5wc8CJkdrTnZ2dnUVZ_jKdnZ2d@posted.palinacquisition> (permalink) |
| Lines | 47 |
| X-Usenet-Provider | http://www.giganews.com |
| NNTP-Posting-Host | 50.46.118.188 |
| X-Trace | sv3-uw0F2Y0qBXe2OCZaM2ynneCvUlaUd+Fuz4h4ja3DICtTFJoM9Lcxsf26FcO4Vcsw+UahjY7pSza6ti2!nRf1CY7z0PSTo895Z5fWyhjyFIFkNssGfq1FXwk+TC2KirMUOIxE6Ry/bEn9E5KYoL26h32DvJSM!y3cln8y8/EItg8kwYPEgsUVdAxi3DLV33VxVJcHOYS4= |
| X-Complaints-To | abuse@iinet.com |
| X-DMCA-Complaints-To | abuse@iinet.com |
| X-Abuse-and-DMCA-Info | Please be sure to forward a copy of ALL headers |
| X-Abuse-and-DMCA-Info | Otherwise we will be unable to process your complaint properly |
| X-Postfilter | 1.3.40 |
| X-Original-Bytes | 2884 |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7094 |
Show key headers only | View raw
On 8/13/11 1:56 PM, vbhavsar@gmail.com wrote:
> [...]
> 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?
It depends on what's legal.
For some kinds of singletons, it is not harmful to initialize the
instance multiple times. There's neither a performance nor interference
issue. For those kinds of singletons, your proposal is fine.
But for others, one of the reasons the class is a singleton in the first
place is that something bad will happen if more than one instance is
even created, never mind used. In those cases, the code you posted is
broken.
Frankly, there is rarely any need to be any more "clever" than to just
create the singleton instance in the static field initializer. Let the
JVM deal with the threading issues automatically and leave it at that.
I'd never even heard of the "single-element enum type" variation of
singleton initialization. That one in particular sounds like a
completely over-engineered approach.
Pete
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
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