servlet是否存在线程安全问题
今天老师问了一句,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 关键字 转载请标明出处: servlet是否存在线程安全问题 文章来源: servlet是否存在线程安全问题