关于JSP 实例方法的线程安全

我测试了下面的代码段,可还是得不出个结论来,还请各位大虾指点迷津。
代码如下:
--------
<%--
Copyright (c) 2002 by Phil Hanna
All rights reserved.

You may study, use, modify, and distribute this
software for any purpose provided that this
copyright notice appears in all copies.

This software is provided without warranty
either expressed or implied.
--%>
<%@ page import="java.text.*" %>
<%@ page import="java.util.*" %>
<%!
// int c;
// static public int sum(int a, int b)
public int sum(int a, int b)
{
int c = 0;

try{
Thread.sleep(350);
}
catch(Exception e)
{
e.printStackTrace();
}

c = a + b;

try{
Thread.sleep(750);
}
catch(Exception e)
{
e.printStackTrace();
}

return c;
}
%>

<%
String requestTime =
new SimpleDateFormat("hh:mm:ss ").format(new Date());

for (int i = 0; i < 22; i++) {
%>
Request at <%= requestTime %> 2 + 2 = <%= sum(2,2) %><br>
<%
// Thread.sleep(750);
}
%>

请问在sum()方法中的变量c,是否存在线程安全问题? 如果c是定义在
JSP Declaration 部分的类的对象时,是否也存在线程安全问题?

如能指教,本人不胜感激。

Jsp中写入Java代码已经扔进历史垃圾堆了,你的问题没有讨论意义。



大家对实例变量的线程安全问题是知道的?可是实例方法中的变量有如何呢? 我测试了下面的代码段,可还是得不出个结论来,还请各位大虾指点迷津。
代码如下:
<%-- Copyright (c) 2002 by Phil Hanna All rights reserved. You may study, use, modify, and distribute this software for any purpose provided that this copyright notice appears in all copies. This software is provided without warranty either expressed or implied.--%><%@ page import="java.text.*" %><%@ page import="java.util.*" %><%! // int c; // static public int sum(int a, int b) public int sum(int a, int b) { int c = 0; try{ Thread.sleep(350); } catch(Exception e) { e.printStackTrace(); } c = a + b; try{ Thread.sleep(750); } catch(Exception e) { e.printStackTrace(); } return c; }%><% String requestTime = new SimpleDateFormat("hh:mm:ss ").format(new Date()); for (int i = 0; i < 22; i++) {%>Request at <%= requestTime %> 2 + 2 = <%= sum(2,2) %><br><% // Thread.sleep(750); }%>

请问在sum()方法中的变量c,是否存在线程安全问题? 如果c是定义在
JSP Declaration 部分的类的对象时,是否也存在线程安全问题?

如能指教,本人不胜感激。

<%! int c=0;
int i;
int sum(int a,int b){
int d=9;
}
%>

sum中的变量a、b、d是线程安全的;
c就不一定是线程安全的;c只有第一此调用的时候初始化;
如果你的c在声明初始化以后不可能改变则是线程安全的;如果c被其它函数或者jsp代码改变则不是线程安全的;比如i,如果你想它是一个循环变量,可以在jsp代码中被改变,那么这是不安全的;因为第一个线程中i变为5的时候,线程等待,而另一个线程得到执行把i初始化为0;如果这个时候第一个线程得到执行则当前i是0而不再是5