posts - 188,comments - 176,trackbacks - 0

 

JSP默认是以多线程方式执行的,这是JSP与ASP,PHP,PERL等脚本语言不一样的地方,也是它的优势之一,但如果不注意多线程中的同步问题,会使所写的JSP程序有难以发现的错误。下面以一个例子说明JSP中的多线程问题及解决方法。

 

一、JSP的中存在的多线程问题:

当客户端第一次请求某一个JSP文件时,服务端把该JSP编译成一个CLASS文件,并创建一个该类的实例,然后创建一个线程处理CLIENT端的请求。如果有多个客户端同时请求该JSP文件,则服务端会创建多个线程。每个客户端请求对应一个线程。以多线程方式执行可大大降低对系统的资源需求,提高系统的并发量及响应时间.对JSP中可能用的的变量说明如下:

实例变量


实例变量是在堆中分配的,并被属于该实例的所有线程共享,所以不是线程安全的.


JSP系统提供的8个类变量


JSP中用到的OUT,Request,Response,Session,Config,Page,PageConxt是线程安全的,Application在整个系统内被使用,所以不是线程安全的.


局部变量


局部变量在堆栈中分配,因为每个线程都有它自己的堆栈空间,所以是线程安全的.


静态类


静态类不用被实例化,就可直接使用,也不是线程安全的.


外部资源:


在程序中可能会有多个线程或进程同时操作同一个资源(如:多个线程或进程同时对一个文件进行写操作).此时也要注意同步问题.

二、下面的例子存在的多线程问题:

<%@ page import="
javax.naming.*,
java.util.*,
java.sql.*,
weblogic.common.*
" %>

<%@ page import=" 
Javax.naming.*
java.util.
*
java.sql.
*
Weblogic.common.
* 
" %> 



<% 
String name 
String product; 
long quantity; 


name
=request.getParameter("name"); 
product
=request.getParameter("product"); 
quantity
=request.getParameter("quantity"); /*(1)*/ 
savebuy(); 
%> 


<%! 
public void savebuy() 

/*进行数据库操作,把数据保存到表中*/ 
try 
Properties props 
= new Properties(); 
props.put(
"user","scott"); 
props.put(
"password","tiger"); 
props.put(
"server","DEMO"); 

Driver myDriver 
= (Driver) iver").newInstance(); 
conn = myDriver.connect("JDBC:weblogic:Oracle", props); 
stmt 
= conn.createStatement(); 

String inssql 
= "insert into buy(empid, name, dept) values (?, ?, ?,?)"
stmt 
= conn.prepareStatement(inssql); 

stmt.setString(
1, name); 
stmt.setString(
2, procuct); 
stmt.setInt(
3, quantity); 
stmt.execute(); 
}
 
catch (Exception e) 

System.out.println(
"SQLException was thrown: " + e.getMessage()); 
}
 
finally //close connections and { 
try 
if(stmt != null
stmt.close(); 
if(conn != null
conn.close(); 
}
 catch (SQLException sqle) 
System.out.println(
"SQLException was thrown: " + sqle.getMessage()); 
}
 
}
 

%> 

<%!
public void  savebuy()
{
    /*进行数据库操作,把数据保存到表中*/
    try {
      Properties props = new Properties();
      props.put("user","scott");
      props.put("password","tiger");
      props.put("server","DEMO");  

      Driver myDriver = (Driver) iver").newInstance();
      conn = myDriver.connect("jdbc:weblogic:oracle", props);
      stmt = conn.createStatement();
   
      String inssql = "insert into buy(empid, name, dept) values (?, ?, ?,?)";
      stmt = conn.prepareStatement(inssql);

      stmt.setString(1, name);
      stmt.setString(2, procuct);   
      stmt.setInt(3, quantity);
      stmt.execute();
    }
    catch (Exception e)
    {
        System.out.println("SQLException was thrown: " + e.getMessage());
    }
    finally //close connections and     {
        try {
          if(stmt != null)
            stmt.close();
          if(conn != null)
            conn.close();
        } catch (SQLException sqle) {
            System.out.println("SQLException was thrown: " + sqle.getMessage());
        }
    }
}
%>

上面的程序模拟网上购物中的一部分,把用户在浏览器中输入的用户名,购买的物品名称,数量保存到表BUY中。在savebuy()函数中用到了实例变量,所以它不是线程安全的.因为:程序中的每一条语句都不是原子操作,如name=request.getParameter("name");在执行是会对应多个机器指令,在任何时候都可能因系统调度而转入睡眠状态,让其他的线程继续执行.如果线程A在执行到(1)的时候转入睡眠状态,线程B开始执行并改变QUANTITY的值,那么当又到A执行时,它会从调用savebuy()函数开始执行,这样它保存到表中的QUANTITY是被线程B改过的值,那么线程A对应的用户所实际购买的数量与保持到表中的数据不一致.这是个很严重的问题.

三、解决方法

 

1.采用单线程方式

在该JSP文件中加上: ,使它以单线程方式执行,这时,仍然只有一个实例,所有客户端的请求以串行方 式执行。这样会降低系统的性能.

 2.对函数savebuy()加synchronized进行线程同步,该JSP仍然以多线程方式执行,但也会降低系统的性能

public synchronized void savebuy() 

        
}
 

 3.采用局部变量代替实例变量

函数savebuy()声明如下:因为在savebuy()中使用的是传给他的形参,是在堆栈中分配的,所以是线程安全的

public void savebuy(String name,String product, int quantity)
{
      
}

调用方式改为:

<% 
String name 
String product; 
long quantity; 
name
=request.getParameter("name"); 
product
=request.getParameter("product"); 
quantity
=request.getParameter("quantity"); 
savebuy(name,product,quantity) 
%> 

如果savebuy的参数很多,或这些数据要在很多地方用到,也可声明一个类,并用他做参数,如:

public class buyinfo
{
      String name;
      String product;
      
long quantity;
}
 
public void savebuy(buyinfo info)
{
      
}

 调用方式改为:

<% 
buyinfo userbuy 
= new buyinfo(); 

userbuy.name
=request.getParameter("name"); 
userbuy.product
=request.getParameter("product"); 
userbuy.quantity
=request.getParameter("quantity"); 
savebuy(userbuy); 
%>

 所以最好是用3,因为1,2会降低系统的性能. 多线程问题一般只有在在大并发量访问时,才有可能出现,并且很难重复出现,所以应在编程时就时刻注意。


转CSDN

posted on 2007-05-24 11:14 cheng 阅读(255) 评论(0)  编辑  收藏 所属分类: JSP/Servlet

只有注册用户登录后才能发表评论。


网站导航: