關於JSP 例項方法的執行緒安全

eserver220發表於2005-06-16
我測試了下面的程式碼段,可還是得不出個結論來,還請各位大蝦指點迷津。
程式碼如下:
--------
<%--
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 部分的類的物件時,是否也存線上程安全問題?

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

相關文章