X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.16.109 Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!nospam.fr.eu.org!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 16 Aug 2011 11:25:22 -0500 Date: Tue, 16 Aug 2011 09:25:20 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; 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> <4o6dnfZWI_mOZtTTnZ2dnUVZ_oqdnZ2d@earthlink.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 26 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.203.65 X-Trace: sv3-HKXO/bGQIPMklbMlzjPhMYRdr5KpVS00DEH9ZR6KgssOmdQm+CJk8P2MjoMUZdsUmyxWaTUV7ZBoi3v!0TgJqW+aTjRjtdtjx+T6QDrFTuivocm3XJyiSwlZpmEgMvdqkDZf2Ydtr5sgTfyc6tX9B59/BHzr!eTWtnaygaEq7H8TYjhYlOAly0o1M8XIFon3fjvoV3xchuQ== 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: 2808 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7136 On 8/16/2011 6:57 AM, Peter Duniho wrote: ... > Or looked at another way: if someone has messed up the class design such > that a singleton class is used even though the singleton instance is > never actually needed, I'm not sure I trust that person to correctly > implement even the basic explicit singleton initialization > synchronization code. Even in that case, better to suffer the potential > performance hit than to allow them to try to get the explicit > initialization right. ... I'm not convinced the method synchronization approach is bad from a performance point of view. I've never seen a getInstance method as a heavy hitter in a profile. I suspect that almost always the synchronization will be uncontended. After all, the method is non-trivial only once per class loader that loads the class. The rest of the time it is one memory access and one conditional branch. If the method is being called frequently, the memory access hits in cache. From the second call on, the conditional branch always goes the same way, so the processor should predict it correctly after a few calls, making it very fast. I do think the static final approach is even simpler. Patricia