合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
![](https://box.kancloud.cn/47cda1396f6fc0190c85ea7526c4d5d4_561x146.png) ![](https://box.kancloud.cn/91661c391be890e00e34051a20e3aad1_351x171.png) ThreadServlet.java ~~~ package zyw.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "ThreadServlet",urlPatterns = "/thread") public class ThreadServlet extends HttpServlet { //int i = 0; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int i = 0; /* synchronized (this){*/ i++;//第一次访问i已经是1,第二次访问=2 try { Thread.sleep(5 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } response.setContentType("text/html;charset=utf-8"); response.getWriter().write(i + ""); /* }*/ } } ~~~