//线程安全(网页)
public class ServletDemo4 extends HttpServlet implements SingleThreadModel{
int i = 0;
//子类覆盖父类的方法,不能抛出比父类更多的异常
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
i++;
try{
Thread.sleep(1000*4);
}
catch(Exception e){
e.printStackTrace();
}
response.getOutputStream().write((i+"").getBytes());
// synchronized (this) {
// i++;
//
// try{
// Thread.sleep(1000*4);
// }
// catch(Exception e){
// e.printStackTrace();
// }
//
// response.getOutputStream().write((i+"").getBytes());
// }
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Servlet的一些细节
最新推荐文章于 2022-10-29 11:51:12 发布