  | Mailing List | | Home | | Forum Home | | JBoss - Java Application Server | | Tomcat - JSP/Servlet container | | Struts - A MVC web framework | | iText - An open source PDF Java Library | | JDOM - JDOM XML Parser | | JSP - A mailing list about Java Server Pages specification and reference | | J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition | | J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog | | Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology | |
Struts & Hibernate
|
|
|
  | | | Singleton pattern | Singleton pattern 2005-10-04 - By Chris Harrison
Back
This works, but is not optimised. You can optimise the method to do lazy instantiation like so:
class Singleton { private static Singleton instance = null; private static Object mutex = new Object();
public Singleton() { }
public static Singleton getInstance() { if (instance == null) { synchonized(mutex) { instance = new Singleton(); } } return instance; } }
Its also tempting to do double check locking, but thats been proven to not work: http://www-128 (See http://www-128.ora-code.com).ibm.com/developerworks/java/library/j-dcl.html
ch.
-- --Original Message-- -- From: An interest list for Sun Java Center J2EE Pattern Catalog [ mailto :J2EEPATTERNS-INTEREST@(protected)]On Behalf Of Harish Sent: Tuesday, 4 October 2005 3:17 PM To: J2EEPATTERNS-INTEREST@(protected) Subject: Re: Singleton pattern
Hi, The code works fine for multithreaded access to the getInstance() method. However, when you analyze it you realize that synchronization is required only for the first invocation of the method. Subsequent invocations do not require synchronization because the first invocation is the only invocation that executes the code at //2,( see below) which is the only line that requires synchronization. All other invocations determine that instance is non-null and return it. Multiple threads can safely execute concurrently on all invocations except the first. However, because the method is synchronized, you pay the cost of synchronization for every invocation of the method, even though it is only required on the first invocation. Another way is to Forgo synchronization and use a static field. class Singleton { private static Singleton instance = new Singleton(); private Singleton() { //... } public static Singleton getInstance() { return instance; } } The code in Listing 10 does not use synchronization and ensures that the Singleton object is not created until a call is made to the static getInstance( ) method. This is a good alternative if your objective is to eliminate synchronization Thanks Harish
-- -- Original Message -- -- From: "SUBSCRIBE EJB-INTEREST anonymous" <ahtwog@(protected)> To: <J2EEPATTERNS-INTEREST@(protected)> Sent: Tuesday, October 04, 2005 8:06 AM Subject: Singleton pattern
> Hi, > Is the below singleton pattern thread safe. > public class Singleton > { > private static Singleton singleton ; > public synchronized static Singleton getInstance() throws > { > if(singleton ==null) > { > singleton = new Singleton (); ////// >>> 2 > } > > return singleton ; > } > > private Singleton () { > //perform member variable initialization here > } > > ==================================================================== > Companion Site: http://www.corej2eepatterns.com > J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns > List Archive: http://archives.java.sun.com/archives/j2eepatterns-interest.html > Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected) > ==================================================================== Companion Site: http://www.corej2eepatterns.com J2EE BluePrints: http://java .sun.com/blueprints/corej2eepatterns List Archive: http://archives.java.sun.com /archives/j2eepatterns-interest.html Unsubscribing: email "signoff J2EEPATTERNS -INTEREST" to listserv@(protected)
The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and permanently delete the material from your computer system. We cannot guarantee that this e-mail is virus-free. You should scan attachments with the latest virus scan before opening. We will not be liable for any loss, cost or damage of any kind whatsoever caused by any receipt or use of this e -mail and attachments.
==================================================================== Companion Site: http://www.corej2eepatterns.com J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns List Archive: http://archives.java.sun.com/archives/j2eepatterns-interest.html Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859 (See http://iso-8859.ora-code.com)-1"> <TITLE></TITLE>
<META content="MSHTML 6.00.2900.2627" name=GENERATOR></HEAD> <BODY> <P><FONT size=2>This works, but is not optimised. You can optimise the method to do lazy instantiation like so:<BR><BR>class Singleton<BR>{<BR> private static Singleton instance = null;<BR> private static Object mutex = new Object();<BR><BR> public Singleton()<BR> {<BR> }<BR><BR> public static Singleton getInstance()<BR> {<BR> if (instance == null)<BR> {<BR> synchonized(mutex)<BR> {<BR> instance = new Singleton();<BR> }<BR> }<BR> return instance;<BR> }<BR>}<BR><BR>Its also tempting to do double check locking, but thats been proven to not work:<BR><A href="http://www-128 (See http://www-128.ora-code.com).ibm.com/developerworks/java/library/j-dcl.html" target=_blank>http://www-128 (See http://www-128.ora-code.com).ibm.com/developerworks/java/library/j-dcl.html</A> <BR></FONT></P> <P><FONT size=2>ch.</FONT></P> <P><FONT size=2><BR>-- --Original Message-- --<BR>From: An interest list for Sun Java Center J2EE Pattern Catalog [<A href="mailto:J2EEPATTERNS-INTEREST@(protected)">mailto:J2EEPATTERNS-INTEREST @(protected)</A>]On Behalf Of Harish<BR>Sent: Tuesday, 4 October 2005 3:17 PM<BR>To: J2EEPATTERNS-INTEREST@(protected)<BR>Subject: Re: Singleton pattern<BR><BR><BR>Hi,<BR>The code works fine for multithreaded access to the getInstance() method. However, when you analyze it you realize that synchronization is required only for the first invocation of the method. Subsequent invocations do not require synchronization because the first invocation is the only invocation that executes the code at //2,( see below) which is the only line that requires synchronization. All other invocations determine that instance is non-null and return it. Multiple threads can safely execute concurrently on all invocations except the first. However, because the method is synchronized, you pay the cost of synchronization for every invocation of the method, even though it is only required on the first invocation.<BR>Another way is to Forgo synchronization and use a static field.<BR>class Singleton<BR>{<BR>private static Singleton instance = new Singleton();<BR> private Singleton()<BR> {<BR> //...<BR> }<BR> public static Singleton getInstance()<BR> {<BR> return instance;<BR> }<BR>}<BR>The code in Listing 10 does not use synchronization and ensures that the Singleton object is not created until a call is made to the static getInstance() method. This is a good alternative if your objective is to eliminate synchronization<BR>Thanks<BR>Harish<BR><BR>-- -- Original Message -- --<BR>From: "SUBSCRIBE EJB-INTEREST anonymous" <ahtwog@(protected)><BR>To: <J2EEPATTERNS-INTEREST@(protected)><BR>Sent: Tuesday, October 04, 2005 8:06 AM<BR>Subject: Singleton pattern<BR><BR><BR>> Hi,<BR>> Is the below singleton pattern thread safe.<BR>> public class Singleton<BR>> {<BR>> private static Singleton singleton ;<BR>> public synchronized static Singleton getInstance() throws<BR>> {<BR>> if(singleton ==null)<BR>> {<BR>> singleton = new Singleton (); ////// >>> 2<BR>> }<BR>><BR>> return singleton ;<BR>> }<BR>><BR>> private Singleton () {<BR>> //perform member variable initialization here<BR>> }<BR>><BR>> ====================================================================<BR>> Companion Site: <A href="http://www.corej2eepatterns.com" target=_blank>http://www.corej2eepatterns.com</A><BR>> J2EE BluePrints: <A href="http://java.sun.com/blueprints/corej2eepatterns" target=_blank>http://java.sun.com/blueprints/corej2eepatterns</A><BR>> List Archive: <A href="http://archives.java.sun.com/archives/j2eepatterns-interest.html" target=_blank>http://archives.java.sun.com/archives/j2eepatterns-interest.html< /A><BR>> Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected)<BR>> ==================================================================== Companion Site: <A href="http://www.corej2eepatterns.com" target=_blank>http://www.corej2eepatterns.com</A> J2EE BluePrints: <A href="http://java.sun.com/blueprints/corej2eepatterns" target=_blank>http://java.sun.com/blueprints/corej2eepatterns</A> List Archive: <A href="http://archives.java.sun.com/archives/j2eepatterns-interest.html" target=_blank>http://archives.java.sun.com/archives/j2eepatterns-interest.html< /A> Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected) </FONT></P></BODY></HTML>
<table><tr><td bgcolor=#ffffff><font color=#000000>The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and permanently delete the material from your computer system. We cannot guarantee that this e-mail is virus-free. You should scan attachments with the latest virus scan before opening. We will not be liable for any loss, cost or damage of any kind whatsoever caused by any receipt or use of this e-mail and attachments.<br> </font></td></tr></table> ==================================================================== Companion Site: http://www.corej2eepatterns.com J2EE BluePrints: http://java.sun.com/blueprints/corej2eepatterns List Archive: http://archives.java.sun.com/archives/j2eepatterns-interest.html Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to listserv@(protected)
|
|
 |