Nitin,
I don't think it needs to be synchronized since all of the variables for
getSquare are local. You only need to make something synchronized when
more than one thread might access a shared resource.
For example:
int sharedVar=0;
public synchronized int someMethod() {
sharedVar+=5;
return sharedVar;
}
In that case we can imagine more than one client wanting to call
someMethod(). If it was not synchronized then more than one thread could
call the method at once. This could yield unpredictable values (i.e. --
you could call the method expecting to get a number five higher, but
really get it back 10 higher since some other thread may have
incremented sharedVar as well).
Hope that helps! If you need more info, check out the Java Language
Specification for a really detailed discussion of threads in Java. For
simpler stuff, check out the tutorials on java.sun.com.
-Matt Tucker
Nitin Mangtani wrote:
>
> (1) I have a servlet
>
> class TestServlet extends HttpServlet
> {
>
> public void doPost(res,req)
> {
> int val = req.getObject(); //not exact sytanx,
> basically this value is send by client.
> int sqval = getSquare(val);
>
> }
>
> int getSquare(int i)
> {
> int sqval = i*i:
> return sqval;
>
> }
> }
>
> I wanted to ask should my getSquare method be synchronized, if not then
> why. Because as far as my understanding goes whenever the new client
> request comes new thread is created only for service methods and not
> other methods.
>
> (2) I have a servlet and separate class.
>
> class TestServlet extends HttpServlet
> {
>
> public void init(ServletConfig config)
> throws ServletException
> {
> super.init(config);
> Square s1 = new Square();
>
> }
> public void doPost(res,req)
> {
> int val = req.getObject(); //not exact sytanx,
> basically this value is send by client.
> int sqval = s1.getSquare(val);
>
> }
>
> }
>
> public class Square
> {
> public int getSquare(int i)
> {
> int sqval = i*i:
> return sqval;
>
> }
> }
> Should I in this case make getSquare as synchronized method.
>
> Regards Nitin.
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
--
-------------------------------
Matt Tucker
[EMAIL PROTECTED]
http://cs.uiowa.edu/~matucker
See CoolServlets:
http://www.coolservlets.com
-------------------------------
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html