Java Mailing List Archive

http://www.junlu.com/

Google
Google
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
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Subject: Servlet : Session invalidate
Oracle Connection Pooling in 3 2 2
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Subject: Running a Simple JMS Example
Tomcat and webapplication specific java library path
Mapping in workers2 properties
org apache jasper JasperException
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action
   MESSAGE
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
Value attribute of <html:checkbox
url string for connecting jboss to oracle
javax servlet ServletException: BeanUtils populate
5 0 18: Windows XP Pro vs Windows 2000
HTTP Status 404 The requested resource is not available
 
Singleton pattern

Singleton pattern

2005-10-04       - By Dan Marchant

 Back
Reply:     1     2     3     4     5     6     7     8  

Supposively it doesn't work on most modern JVMs though.
But unless you are doing real-time programming with java you will
probably never notice it breaking. In the webapp environment, use a
servlet to initialize your singletons and this will usually prevent
any double-checked locking issues since the webapp will not be
available until it is initialized there by preventing business logic
to run from user behavior. You can set the servlet to be initialized
on webapp deployment within your web.xml.

Article on why this code example is broken:
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

Bug from sun themselves:
http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_idc12351

Hope this helps.

Dan

On 10/4/05, bayar <bayar@(protected)> wrote:
> as ibm article says, the true one is;
>
>  1.        public static Singleton getInstance()
>  2.        {
>  3.                if (instance == null)
>  4.                {
>  5.                             synchonized(mutex)
>  6.                     {
>                                         if (instance == null)
>                                         {
>  7.                                     instance = new Singleton();
>                                         }
>  8.                     }
>  9.                }
> 10.                return instance;
> 11.        }
> 12.   }
>
>
> -- --Original Message-- --
> From: An interest list for Sun Java Center J2EE Pattern Catalog
> [mailto:J2EEPATTERNS-INTEREST@(protected)]On Behalf Of Stefano Greco
> Sent: Tuesday, October 04, 2005 2:45 PM
> To: J2EEPATTERNS-INTEREST@(protected)
> Subject: Re: Singleton pattern
> Importance: High
>
>
> Hi,
> I don't think it's a correct.
>
> in the method
>
>  1.        public static Singleton getInstance()
>  2.        {
>  3.                if (instance == null)
>  4.                {
>  5.                        synchonized(mutex)
>  6.                        {
>  7.                                instance = new Singleton();
>  8.                        }
>  9.                }
> 10.                return instance;
> 11.        }
> 12.   }
>
> Two or more threads may valuate the if statement in line 3 and they may have
> all condition to "true"
> then all threads stop on line 5 and "only one" make the instance of the
> Singleton.
>
> But, when the "only one" 's finished and exit from the synchronized lines
> another thread make the instance of the Singleton
> on so all other threads.
>
>
> bye Stefano
>
> >-- Messaggio Originale --
> >Date:         Tue, 4 Oct 2005 15:40:39 +1000
> >Reply-To: An interest list for Sun Java Center J2EE Pattern Catalog
> <J2EEPATTERNS-INTEREST@(protected)>
> >From: Chris Harrison <CHarrison@(protected)>
> >Subject: Re: Singleton pattern
> >To: J2EEPATTERNS-INTEREST@(protected)
> >
> >
> >
> >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)
>
>
> __ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ___
> TISCALI ADSL
> Solo con Tiscali Adsl navighi senza limiti e telefoni senza canone
> Telecom a partire da 19,95 Euro/mese.
> Attivala subito, I PRIMI DUE MESI SONO GRATIS! CLICCA QUI:
> http://abbonati.tiscali.it/adsl/sa/1e25flat_tc/
>
> ===================================================================>
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)
>

===================================================================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)

©2008 junlu.com - Jax Systems, LLC, U.S.A.