• servlet的多线程并发问题


    package gz.itcast.e_thread;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     * servlet的多线程并发问题
     * @author APPle
     *
     */
    public class TheradDemo extends HttpServlet {
        
        int count = 1;
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=utf-8");
            
            synchronized (TheradDemo.class) {//锁对象必须唯一。建议使用类对象
                response.getWriter().write("你现在是当前网站的第"+count+"个访客");   //线程1执行完  , 线程2执行
            
            //线程1还没有执行count++
            /*try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
                count++;
            }
        }
    
    }
  • 相关阅读:
    植物:柏树
    植物:水杉
    植物:珙桐
    植物:桫椤
    汉语-成语:悠闲自在
    植物:孑遗植物
    汉语-词语:孑遗
    汉语-词语:调味品
    调味品:酱油
    netstat 命令详解
  • 原文地址:https://www.cnblogs.com/tzzt01/p/7326653.html
Copyright © 2020-2023  润新知