servlet是否存在线程安全问题

匿名 (未验证) 提交于 2019-12-03 00:26:01

        今天老师问了一句,servlet存在线程安全问题吗,

public class threadSafe extends HttpServlet{     private static final long serialVersionUID = 1L;     private volatile int num = 0;     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         addOne();         response.getWriter().write("now access num : " + getNum());     }     /**       * 读取开销低       */      private int getNum() {         return num;     }     /**       * 其写入为非线程安全的,赋值操作开销高       */      private synchronized void addOne() {         num ++;     } }



synchronized关键字

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!