我正在学习struts,并创建了一个dispatchAction来检查用户特权,然后转发到正确的页面。
这是代码:

public class UserCheck extends DispatchAction {

    private static String role = "";

    public class UserAction extends DispatchAction {


        public ActionForward checPrivileges(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException {
            boolean isAdmin;
            //check if admin and put outputin in isAdmin
            //check if user is admin
            if (isAdmin) {
                role = "admin";
            } else {
                role = "user";
            }
               //forwared based on role
        }
    }


由于属性角色是静态的并且在UserCheck的所有实例之间共享,因此该代码能否导致争用情况?

最佳答案

是的,如果发生以下情况,将导致竞争状况:
1.您有多个UserAction实例。
2.当您只有一个UserAction实例时,而checPrivileges不是同步方法。

关于java - 此代码可以导致竞争状态吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35221185/

10-14 04:44