基于线程的servlet本身就是多线程的,
只能让线程安全,却没办法实现servlet的单线程!

解决方案 »

  1.   

    你让Servlet单线程有什么意义么?你的网站每次只让一个人访问?
      

  2.   

    Servlet不能单线程。另外有单线程模式吗?单例模式吧?单例不是单线程
      

  3.   

    Servlet好像是多线程,并线程不安全的吧?
      

  4.   

    servlet是法实现单线程的,只能线程安全,实现javax.servlet.SingleThreadModel就好了,这个接口没有方法,但是在
    2.4版本中已经不推荐使用了Deprecated. As of Java Servlet API 2.4, with no direct replacement. public interface SingleThreadModelEnsures that servlets handle only one request at a time. This interface has no methods. If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet. Note that SingleThreadModel does not solve all thread safety issues. For example, session attributes and static variables can still be accessed by multiple requests on multiple threads at the same time, even when SingleThreadModel servlets are used. It is recommended that a developer take other means to resolve those issues instead of implementing this interface, such as avoiding the usage of an instance variable or synchronizing the block of the code accessing those resources. This interface is deprecated in Servlet API version 2.4. 
      

  5.   

    在jsp页面中做如下设置:<%@ page isThreadSafe="false" %>
      

  6.   

    <%@ page isThreadSafe="true|false" %>
    默认值为trueisThreadSafe=false模式表示它是以Singleton模式运行。
         该模式implements了接口SingleThreadMode,
         该模式同一时刻只有一个实例,不会出现信息同步与否的概念。
         若多个用户同时访问一个这种模式的页面,
         那么先访问者完全执行完该页面后,后访问者才开始执行。isThreadSafe=true模式表示它以多线程方式运行。
        该模式的信息同步,需访问同步方法(用synchronized标记的)来实现。
        一般格式如下:
        public synchronized void syncmethod(...){
          if(...) {
             wait();
          }
          notifyAll();
        } 
      

  7.   

    方法:<%@ page isThreadSafe="false" %>