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
 
Subject: Is Singleton thread safe?

Subject: Is Singleton thread safe?

2005-04-01       - By Patrick Blais

 Back
Reply:     1     2     3     4     5     6     7     8     9     10     >>  

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>&nbsp;super();<BR>}<BR>public static SequenceManager getInstance() {</DIV>
<DIV>if(manager == null){<BR>&nbsp;manager= new SequenceManager();<BR>&nbsp
;return manager;<BR>}else{<BR>&nbsp;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>&nbsp;super();<BR>}</DIV>
<DIV>public static SequenceManager getInstance() {<BR>&nbsp;return manager;<BR>
}<BR></DIV>
<DIV>&nbsp;</DIV>
<DIV><B><I>Sureshbabu </I></B></DIV>
<DIV><B><I>Koya &lt;Sureshbabu.Koya@(protected)&gt;</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>&nbsp;</DIV>
<DIV><SPAN class=062503416-01042005><FONT face=Arial color=#0000ff size=2>I saw
people using synchonize blocks&nbsp;for servlet&nbsp;instance variables instead
of maintaining local variables. Since servlet is a singleton this is necessary.
&nbsp;Apart from this case(Singleton) I&nbsp;did not&nbsp;see people&nbsp;trying
to synchronize static variables.&nbsp;&nbsp;</FONT></SPAN></DIV>
<DIV><SPAN class=062503416-01042005><FONT face=Arial color=#0000ff size=2><
/FONT></SPAN>&nbsp;</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>&nbsp;</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.&nbsp; Could I Synchronized static varible?<BR><BR><B><I
>Sureshbabu Koya &lt;Sureshbabu.Koya@(protected)&gt;</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>&nbsp;</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>&nbsp;</DIV>
<DIV>Is Singleton thread safe? Why?</DIV>
<DIV>&nbsp;</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&nbsp; Liu&nbsp;
</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&nbsp; Liu&nbsp;
</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)

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