  | 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
|
|
|
  | | | Subject: Is Singleton thread safe? | Subject: Is Singleton thread safe? 2005-04-01 - By Patrick Blais
Back It depends on the way you implement it. You might have thread safe issues if you have per say the following class: public class SequenceManager { private static SequenceManager manager; protected SequenceManager () { super(); } public static SequenceManager getInstance() { if(manager == null){ manager= new SequenceManager(); return manager; }else{ return manager; } is not thread safe. The following implementation is thread safe. THe difference is when the class loader will load the class, it will instanciate the variable manager and this, will be done only once. You do not need to synchronize the method... yoou need to prevent from loading the class twice. public class SequenceManager { private static SequenceManager manager=new SequenceManager(); protected SequenceManager () { super(); } public static SequenceManager getInstance() { return manager; }
Sureshbabu Koya <Sureshbabu.Koya@(protected)> wrote: You cannot syncronize a static variable. It is always better if you modify your variables using set/get methods. Once you syncronize your set method your variable would be thread safe.
I saw people using synchonize blocks for servlet instance variables instead of maintaining local variables. Since servlet is a singleton this is necessary. Apart from this case(Singleton) I did not see people trying to synchronize static variables.
Regards, Suresh Koya
-- --Original Message-- -- From: An interest list for Sun Java Center J2EE Pattern Catalog [mailto :J2EEPATTERNS-INTEREST@(protected)]On Behalf Of Julia Liu Sent: Friday, April 01, 2005 9:47 PM To: J2EEPATTERNS-INTEREST@(protected) Subject: Re: Is Singleton thread safe?
Thanks for reply. Could I Synchronized static varible?
Sureshbabu Koya <Sureshbabu.Koya@(protected)> wrote: Making a singleton thread safe is up to you. You should also think about how the performance get a hit if you want to make a singleton threadsafe.
regards, Suresh -- --Original Message-- -- From: An interest list for Sun Java Center J2EE Pattern Catalog [mailto :J2EEPATTERNS-INTEREST@(protected)]On Behalf Of Julia Liu Sent: Friday, April 01, 2005 9:24 PM To: J2EEPATTERNS-INTEREST@(protected) Subject: Is Singleton thread safe?
Hi,
Is Singleton thread safe? Why?
Thanks
Julia Liu
-- ---- ---- ---- ---- ---- ----- Yahoo! Messenger Show us what our next emoticon should look like. Join the fun. ================ ==================================================== 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)
Julia Liu
-- ---- ---- ---- ---- ---- ----- Yahoo! Messenger Show us what our next emoticon should look like. Join the fun. ================ ==================================================== 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)
-- ---- ---- ---- ---- ---- ----- Do you Yahoo!? Make Yahoo! your home page
==================================================================== 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) <DIV>It depends on the way you implement it.<BR>You might have thread safe issues if you have per say the following class:</DIV> <DIV>public class SequenceManager {</DIV> <DIV>private static SequenceManager manager;<BR>protected SequenceManager () { <BR> super();<BR>}<BR>public static SequenceManager getInstance() {</DIV> <DIV>if(manager == null){<BR> manager= new SequenceManager();<BR>  ;return manager;<BR>}else{<BR> return manager;<BR>}</DIV> <DIV>is not thread safe.</DIV> <DIV>The following implementation is thread safe. THe difference is when the class loader will load the class,</DIV> <DIV>it will instanciate the variable manager and this, will be done only once. You do not need</DIV> <DIV>to synchronize the method... yoou need to prevent from loading the class twice.</DIV> <DIV>public class SequenceManager {</DIV> <DIV>private static SequenceManager manager=new SequenceManager();<BR>protected SequenceManager () {<BR> super();<BR>}</DIV> <DIV>public static SequenceManager getInstance() {<BR> return manager;<BR> }<BR></DIV> <DIV> </DIV> <DIV><B><I>Sureshbabu </I></B></DIV> <DIV><B><I>Koya <Sureshbabu.Koya@(protected)></I></B> wrote:</DIV> <BLOCKQUOTE class=replbq style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER -LEFT: #1010ff 2px solid"> <META content="MSHTML 6.00.2600.0" name=GENERATOR> <DIV><SPAN class=062503416-01042005><FONT face=Arial color=#0000ff size=2>You cannot syncronize a static variable. It is always better if you modify your variables using set/get methods. Once you syncronize your set method your variable would be thread safe.</FONT></SPAN></DIV> <DIV><SPAN class=062503416-01042005><FONT face=Arial color=#0000ff size=2>< /FONT></SPAN> </DIV> <DIV><SPAN class=062503416-01042005><FONT face=Arial color=#0000ff size=2>I saw people using synchonize blocks for servlet instance variables instead of maintaining local variables. Since servlet is a singleton this is necessary. Apart from this case(Singleton) I did not see people trying to synchronize static variables. </FONT></SPAN></DIV> <DIV><SPAN class=062503416-01042005><FONT face=Arial color=#0000ff size=2>< /FONT></SPAN> </DIV> <DIV><SPAN class=062503416-01042005><FONT face=Arial color=#0000ff size=2 >Regards,</FONT></SPAN></DIV> <DIV><SPAN class=062503416-01042005><FONT face=Arial color=#0000ff size=2 >Suresh Koya</FONT></SPAN></DIV> <DIV><SPAN class=062503416-01042005></SPAN><FONT face=Tahoma><FONT face=Arial color=#0000ff size=2></FONT><FONT face=Arial color=#0000ff size=2></FONT><FONT face=Arial color=#0000ff size=2></FONT><BR><FONT size=2><SPAN class=062503416 -01042005><FONT face=Arial color=#0000ff> </FONT></SPAN>-- --Original Message-- --<BR><B>From:</B> An interest list for Sun Java Center J2EE Pattern Catalog [mailto:J2EEPATTERNS-INTEREST@(protected)]<B>On Behalf Of </B>Julia Liu<BR><B>Sent:</B> Friday, April 01, 2005 9:47 PM<BR><B>To:</B> J2EEPATTERNS -INTEREST@(protected)<BR><B>Subject:</B> Re: Is Singleton thread safe?<BR><BR>< /FONT></DIV></FONT> <BLOCKQUOTE style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid"> <DIV>Thanks for reply. Could I Synchronized static varible?<BR><BR><B><I >Sureshbabu Koya <Sureshbabu.Koya@(protected)></I></B> wrote: <BLOCKQUOTE class=replbq style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER -LEFT: #1010ff 2px solid"> <META content="MSHTML 6.00.2600.0" name=GENERATOR> <DIV><SPAN class=405301116-01042005><FONT face=Arial color=#0000ff size=2 >Making a singleton thread safe is up to you. You should also think about how the performance get a hit if you want to make a singleton threadsafe.</FONT>< /SPAN></DIV> <DIV><SPAN class=405301116-01042005><FONT face=Arial color=#0000ff size=2>< /FONT></SPAN> </DIV> <DIV><SPAN class=405301116-01042005><FONT face=Arial color=#0000ff size=2 >regards,</FONT></SPAN></DIV> <DIV><SPAN class=405301116-01042005><FONT face=Arial color=#0000ff size=2 >Suresh</FONT></SPAN></DIV> <BLOCKQUOTE style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid"> <DIV class=OutlookMessageHeader dir=ltr align=left><FONT face=Tahoma size=2>--- --Original Message-- --<BR><B>From:</B> An interest list for Sun Java Center J2EE Pattern Catalog [mailto:J2EEPATTERNS-INTEREST@(protected)]<B>On Behalf Of </B>Julia Liu<BR><B>Sent:</B> Friday, April 01, 2005 9:24 PM<BR><B>To:</B> J2EEPATTERNS-INTEREST@(protected)<BR><B>Subject:</B> Is Singleton thread safe? <BR><BR></FONT></DIV> <DIV>Hi,</DIV> <DIV> </DIV> <DIV>Is Singleton thread safe? Why?</DIV> <DIV> </DIV> <DIV>Thanks<BR><BR></DIV><BR><BR> <DIV> <DIV> <DIV> <DIV> <DIV> <DIV> <DIV> <DIV> <DIV><FONT color=#4040ff size=3><EM><STRONG><FONT size=2>Julia Liu </FONT><IMG src="http://us.i1.yimg.com/us.yimg.com/i/mesg/tsmileys2/40.gif" NOSEND="1"></STRONG></EM></FONT></DIV></DIV></DIV></DIV></DIV></DIV></DIV></DIV ></DIV> <P> <HR SIZE=1> Yahoo! Messenger<BR>Show us what our next emoticon should look like. <A href= "http://us.rd.yahoo.com/evt=31855/*http://advision.webevents.yahoo.com /emoticontest">Join the fun.</A> =============================================== ===================== 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)</BLOCKQUOTE>========== ========================================================== 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) </BLOCKQUOTE></DIV><BR><BR> <DIV> <DIV> <DIV> <DIV> <DIV> <DIV> <DIV> <DIV> <DIV><FONT color=#4040ff size=3><EM><STRONG><FONT size=2>Julia Liu </FONT><IMG src="http://us.i1.yimg.com/us.yimg.com/i/mesg/tsmileys2/40.gif" NOSEND="1"></STRONG></EM></FONT></DIV></DIV></DIV></DIV></DIV></DIV></DIV></DIV ></DIV> <P> <HR SIZE=1> Yahoo! Messenger<BR>Show us what our next emoticon should look like. <A href= "http://us.rd.yahoo.com/evt=31855/*http://advision.webevents.yahoo.com /emoticontest">Join the fun.</A> =============================================== ===================== 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)</BLOCKQUOTE>========== ========================================================== 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) </BLOCKQUOTE><p> <hr size=1>Do you Yahoo!?<br> <a href="http://us.rd.yahoo.com/my/navbar/sethp/*http://www.yahoo.com/r/hs" >Make Yahoo! your home page</a>
==================================================================== 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)
|
|
 |