servlet的多線程並發問題
阿新 • • 發佈:2017-08-09
tex syn sync har class nbsp tca read app
package gz.itcast.e_thread; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * servlet的多線程並發問題 * @author APPle * */ public class TheradDemo extendsHttpServlet { int count = 1; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); synchronized (TheradDemo.class) {//鎖對象必須唯一。建議使用類對象 response.getWriter().write("你現在是當前網站的第"+count+"個訪客"); //線程1執行完 , 線程2執行 //線程1還沒有執行count++ /*try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); }*/ count++; } } }
servlet的多線程並發問題