标签:
有状态就是有数据存储功能。有状态对象(Stateful Bean),就是有实例变量的对象,可以保存数据,是非线程安全的。在不同方法调用间不保留任何状态。
无状态就是一次操作,不能保存数据。无状态对象(Stateless Bean),就是没有实例变量的对象.不能保存数据,是不变类,是线程安全的。
public class StatelessServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int integer = 100;
System.out.println("我不影响其他进程");
}
}
public class StatelessServlet extends HttpServlet {
private int count = 0;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int integer = 100;
count++;
System.out.println("我不影响其他进程");
}
}
synchronized(lock){
访问或修改被锁保护的共享状态
}
public class StatelessServlet extends HttpServlet {
private int count = 0;
@Override
protected synchronized void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int integer = 100;
count++;
System.out.println("我不影响其他进程");
}
}
标签:
原文地址:http://www.cnblogs.com/yeziTesting/p/4931167.html