  | 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 Harish
Back 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) <!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"> <META content="MSHTML 6.00.2800.1505" name=GENERATOR> <STYLE></STYLE> </HEAD> <BODY> <DIV> <P><FONT face="Palatino Linotype" size=2>Hi,</FONT></P> <P><FONT face="Palatino Linotype" size=2>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.</FONT></P> <P><FONT face="Palatino Linotype" size=2>Another way is to Forgo synchronization and use a static field.</FONT></P> <P><FONT face="Palatino Linotype" size=2>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 <CODE>Singleton</CODE> object is not created until a call is made to the <CODE>static getInstance()</CODE> method. This is a good alternative if your objective is to eliminate synchronization</FONT></P> <P><FONT face="Palatino Linotype" size=2>Thanks</FONT></P> <P><FONT face="Palatino Linotype" size=2>Harish</FONT></P> <P><FONT face="Palatino Linotype" size=2></FONT> </P></DIV> <DIV><FONT face="Palatino Linotype" size=2>-- -- Original Message -- -- </FONT> <DIV><FONT face="Palatino Linotype" size=2>From: "SUBSCRIBE EJB-INTEREST anonymous" <</FONT><A href="mailto:ahtwog@(protected)"><FONT face="Palatino Linotype" size=2>ahtwog@(protected)</FONT></A><FONT face="Palatino Linotype" size=2>></FONT></DIV> <DIV><FONT face="Palatino Linotype" size=2>To: <</FONT><A href="mailto:J2EEPATTERNS-INTEREST@(protected)"><FONT face="Palatino Linotype" size=2>J2EEPATTERNS-INTEREST@(protected)</FONT></A><FONT face="Palatino Linotype" size=2>></FONT></DIV> <DIV><FONT face="Palatino Linotype" size=2>Sent: Tuesday, October 04, 2005 8:06 AM</FONT></DIV> <DIV><FONT face="Palatino Linotype" size=2>Subject: Singleton pattern</FONT></DIV></DIV> <DIV><FONT face="Palatino Linotype"><BR><FONT size=2></FONT></FONT></DIV><FONT face="Palatino Linotype" size=2>> 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>> <FONT color=#ff0000>singleton = new Singleton (); ////// >>> 2 <BR></FONT>> }<BR>> <BR>> return singleton ;<BR>> }<BR>> <BR>> private Singleton () {<BR>> //perform member variable initialization here<BR>> }<BR>> <BR>> ====================================================================<BR>> Companion Site: </FONT><A href="http://www.corej2eepatterns.com"><FONT face="Palatino Linotype" size=2>http://www.corej2eepatterns.com</FONT></A><BR><FONT face="Palatino Linotype" size=2>> J2EE BluePrints: </FONT><A href="http://java.sun.com/blueprints/corej2eepatterns"><FONT face="Palatino Linotype" size=2>http://java.sun.com/blueprints/corej2eepatterns</FONT></A><BR><FONT face="Palatino Linotype" size=2>> List Archive: </FONT><A href="http://archives.java.sun.com/archives/j2eepatterns-interest.html"><FONT face="Palatino Linotype" size=2>http://archives.java.sun.com/archives/j2eepatterns-interest.html</FONT>< /A><BR><FONT face="Palatino Linotype" size=2>> Unsubscribing: email "signoff J2EEPATTERNS-INTEREST" to </FONT><A href="mailto:listserv@(protected)"><FONT face="Palatino Linotype" size=2>listserv@(protected)</FONT></A><BR><FONT face="Palatino Linotype" size=2>> </FONT></BODY></HTML> ==================================================================== 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)
|
|
 |