servlet的多线程并发问题

时间:2017-08-09 17:56:50   收藏:0   阅读:243
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 extends HttpServlet {
    
    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++;
        }
    }

}

 

原文:http://www.cnblogs.com/tzzt01/p/7326653.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!