  | 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
|
|
|
  | | | is Static methodsl thread safe | is Static methodsl thread safe 2006-03-04 - By Parab, Prashant
Back
Hi Group
The question is whether static methods are thread safe just like instance method.
Consider following example for the discussion.
public class TestFactory
{
public static Object getBean(String beanName)
{
/*
Code to return bean depending on the argument beanName
*/
}
}
I am bit confused about how the method getBean will behave in the multithreaded environment. The point of confusion is, all the static declaration inside a class has only one copy in the memory. Assuming this is correct then there should be only one copy of getBean method in memory. So if two threads are accessing this method simultaneously then they may corrupt the value of argument of each other and the logic will fail.
Based on above discussion kindly let me know your view.
This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.
=========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas -microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">
<head> <meta http-equiv=Content-Type content="text/html; charset=us-ascii"> <meta name=Generator content="Microsoft Word 11 (filtered medium)"> <style> <!-- /* Font Definitions */ @(protected) {font-family:Verdana; panose-1 (See http://ose-1.ora-code.com):2 11 6 4 3 5 4 4 2 4;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman";} a:link, span.MsoHyperlink {color:blue; text-decoration:underline;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline;} span.EmailStyle17 {mso-style-type:personal-compose; font-family:Verdana; color:windowtext; font-weight:normal; font-style:normal; text-decoration:none none;} @(protected) Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in;} div.Section1 {page:Section1;} --> </style>
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'>Hi Group<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'>The question is whether static methods are thread safe just like instance method.<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'>Consider following example for the discussion.<o:p></o:p>< /span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'>public class TestFactory<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'>{<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'> public static Object getBean(String beanName)<o:p></o:p></span></font></p>
<p class=MsoNormal style='text-indent:.5in'><font size=2 face=Verdana><span style='font-size:10.0pt;font-family:Verdana'>{<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'> /*<o:p></o:p></span>< /font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'> Code to return bean depending on the argument beanName<o:p></o:p></span></font></p>
<p class=MsoNormal style='margin-left:.5in;text-indent:.5in'><font size=2 face=Verdana><span style='font-size:10.0pt;font-family:Verdana'>*/ <o:p></o:p>< /span></font></p>
<p class=MsoNormal style='text-indent:.5in'><font size=2 face=Verdana><span style='font-size:10.0pt;font-family:Verdana'>} <o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'>}<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'>I am bit confused about how the method getBean will behave in the multithreaded environment. The point of confusion is, all the static declaration inside a class has only one copy in the memory. Assuming this is correct then there should be only one copy of getBean method in memory. So if two threads are accessing this method simultaneously then they may corrupt the value of argument of each other and the logic will fail.<o:p></o:p></span>< /font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'>Based on above discussion kindly let me know your view. <o :p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Verdana><span style='font-size:10.0pt; font-family:Verdana'><o:p> </o:p></span></font></p>
</div>
</body>
</html>
<table><tr><td bgcolor=#ffffff><font color=#000000>This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof . If you receive this message in error, please notify the sender immediately and delete all copies of this message.<br> <br> </font></td></tr></table> =========================================================================== To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help". <p>
|
|
 |