因此,我有一个带有某些属性的“ Server”类,其中一个是“ joinSession”,默认为布尔值,为false。

我有一个二传手和吸气剂,它工作得很好。但是我的问题是,当有超过1个人连接到我的应用程序时(有时会导致setter函数将“ joinSession”设置为true),他们将共享joinSession的值...因此,如果client2将其更改为true,则client1仍然应该具有默认false值的人,实际上也变为true ...

如果这样做没有意义,并且您需要查看代码,请告诉我。谢谢

代码:(由于NDA,许多非贡献代码已被删除)

public class TunnelServlet {

    Server server = new Server("MY_PATH", "MY_JWT");
    if (request.getParameter("joinSession").equals("true") {
        server.setJoinSession(true);
    }

    System.out.println(server.getJoinSession);
}


和我的服务器类

public class Server {
    private static String path;
    private static String JWToken;
    private static boolean joinSession;

    Server( String domain, String token ) {
        path = "http://" + domain + ":8000/" ;
        JWToken = token;
    }

    public void setJoinSession(boolean isJoinSession) {
        joinSession = isJoinSession;
    }

    public boolean getJoinSession() {
        return joinSession;
    }
}

最佳答案

我认为您的joinSession是一个静态字段,因此它属于您的服务器类,每个人都在更改它。您不应该这样做,因为静态字段属于类而不属于对象。如果每个人都需要此字段,则不应将其设置为静态。这样可以解决您的问题。

07-27 20:26