当多个客户端并发访问同一个Servlet时,web服务器会为每一个客户端的访问请求创建一个线程,并在这个线程上调用Servlet的service方法,因此service方法内如果访问了同一个资源的话,就有
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo3 extends HttpServlet { int i=1; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { i++; try { Thread.sleep(1000*4); } catch (InterruptedException e) { e.printStackTrace(); } response.getWriter().write(i+""); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo3 extends HttpServlet { int i=1; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 加了synchronized后,并发访问i时就不存在线程安全问题了, * 为什么加了synchronized后就没有线程安全问题了呢? * 假如现在有一个线程访问Servlet对象,那么它就先拿到了Servlet对象的那把锁 * 等到它执行完之后才会把锁还给Servlet对象,由于是它先拿到了Servlet对象的那把锁, * 所以当有别的线程来访问这个Servlet对象时,由于锁已经被之前的线程拿走了,后面的线程只能排队等候了 * */ synchronized (this) {//在java中,每一个对象都有一把锁,这里的this指的就是Servlet对象 i++; try { Thread.sleep(1000*4); } catch (InterruptedException e) { e.printStackTrace(); } response.getWriter().write(i+""); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }