如何用SingleThreadModel解决多线程安全问题

用SingleThreadModel解决多线程安全问题:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
前面介绍的都是普通的Servlet。对于每一个用户请求,那些Servlet都会用线程的方式给予应答。这样比较节省系统的资源。Sun公司也给出了另外一种方法,就是这节要介绍的SingleThreadModel的方法。当implement这个接口以后,你的Servlet就变成了另外一种模式工作。即,每一个新用户的请求,都会生成一个新的Servlet实例来应答。这种方法有两个方面的弊病。一是性能太差,最后会把机器拖累死。还有一条就是有时解决不了实际问题。每个servlet类实例都有自己独立的变量。如果我们的本意就是想让客户线程之间进行这些变量的交流。这种方法就无法做到。就像还有人建议的,用局部变量来代替类变量一样,有时也解决不了实际当中的算法问题。因为我们有时就需要用一个类似类变量一样的东西,来控制全局。即使这种方法这不好,那不好,现实中很多很多工程师也说不好。我还是给出了例子,让大家看看结果。

例:2.2.1
package com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ServletHello1 extends HttpServlet implements SingleThreadModel{
    int bookNum=20;
    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PrintWriter pw = response.getWriter();
        while (bookNum>0){
            onlySellOne(pw);
        }
    }
    void onlySellOne(PrintWriter pw) throws IOException {
        if (bookNum > 0) {
            pw.println(Thread.currentThread().getName()
                    + " before" + bookNum);
            pw.flush();
            bookNum--;
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
            }
            pw.println(Thread.currentThread().getName()
                    + " after " + bookNum);
            pw.flush();
        }
    }
}

内置浏览器的输出结果是:

http-8080-Processor22 before20 http-8080-Processor22 after 19 http-8080-Processor22 before19 http-8080-Processor22 after 18 http-8080-Processor22 before18 http-8080-Processor22 after 17 http-8080-Processor22 before17 http-8080-Processor22 after 16 http-8080-Processor22 before16 http-8080-Processor22 after 15 http-8080-Processor22 before15 http-8080-Processor22 after 14 http-8080-Processor22 before14 http-8080-Processor22 after 13 http-8080-Processor22 before13 http-8080-Processor22 after 12 http-8080-Processor22 before12 http-8080-Processor22 after 11 http-8080-Processor22 before11 http-8080-Processor22 after 10 http-8080-Processor22 before10 http-8080-Processor22 after 9 http-8080-Processor22 before9 http-8080-Processor22 after 8 http-8080-Processor22 before8 http-8080-Processor22 after 7 http-8080-Processor22 before7 http-8080-Processor22 after 6 http-8080-Processor22 before6 http-8080-Processor22 after 5 http-8080-Processor22 before5 http-8080-Processor22 after 4 http-8080-Processor22 before4 http-8080-Processor22 after 3 http-8080-Processor22 before3 http-8080-Processor22 after 2 http-8080-Processor22 before2 http-8080-Processor22 after 1 http-8080-Processor22 before1 http-8080-Processor22 after 0




火狐浏览器的输出结果是:
http-8080-Processor24 before20
http-8080-Processor24 after 19
http-8080-Processor24 before19
http-8080-Processor24 after 18
http-8080-Processor24 before18
http-8080-Processor24 after 17
http-8080-Processor24 before17
http-8080-Processor24 after 16
http-8080-Processor24 before16
http-8080-Processor24 after 15
http-8080-Processor24 before15
http-8080-Processor24 after 14
http-8080-Processor24 before14
http-8080-Processor24 after 13
http-8080-Processor24 before13
http-8080-Processor24 after 12
http-8080-Processor24 before12
http-8080-Processor24 after 11
http-8080-Processor24 before11
http-8080-Processor24 after 10
http-8080-Processor24 before10
http-8080-Processor24 after 9
http-8080-Processor24 before9
http-8080-Processor24 after 8
http-8080-Processor24 before8
http-8080-Processor24 after 7
http-8080-Processor24 before7
http-8080-Processor24 after 6
http-8080-Processor24 before6
http-8080-Processor24 after 5
http-8080-Processor24 before5
http-8080-Processor24 after 4
http-8080-Processor24 before4
http-8080-Processor24 after 3
http-8080-Processor24 before3
http-8080-Processor24 after 2
http-8080-Processor24 before2
http-8080-Processor24 after 1
http-8080-Processor24 before1
http-8080-Processor24 after 0

尽管上面两个浏览器同时访问,两个浏览器输出的结果也都是正确的,但也没有用,因为这不是用户需求要求我们的。用户需求要求我们说,总共就24本书,两个人一起卖。而现在这样成了,两个人加起来,一共48本书了。