我有一个执行各种业务逻辑的servlet。我想避免这样的同步:
@Override
protected void doGet(final HttpServletRequest _req, final HttpServletResponse _resp) throws ServletException, IOException {
synchronized (MyServlet.class) {
various();
calls();
and_logic(_req, _resp);
}
}
将所有被调用的方法设为静态并按此执行:
@Override
protected void doGet(final HttpServletRequest _req, final HttpServletResponse _resp) throws ServletException, IOException {
_doGet(_req, _resp);
}
private static void _doGet(final HttpServletRequest _req, final HttpServletResponse _resp) throws ServletException, IOException {
various();
calls();
and_logic(_req, _resp);
}
我不会使用任何静态变量,并且假设我的所有方法调用都是线程安全的。有没有明显的缺点?
答案 0 :(得分:1)
我不会使用任何静态变量,并且假定我的所有方法调用都是线程安全的。
在这些条件下,您既不需要同步也不需要静态方法。只需使用servlet或其他服务类的实例方法。