简体   繁体   English

Getter方法返回两个不同的对象identityHashCodes

[英]Getter Method is returning two different objects identityHashCodes

helping a friend on a project we tried to debug a problem and then encountered an even more puzzling problem. 帮助一个项目的朋友,我们尝试调试问题,然后遇到一个更加令人困惑的问题。 I have a thread get the value of a certain variable via a getter method and print its identityHashCode every 500ms. 我有一个线程通过getter方法获取某个变量的值,并每500ms打印一次其identityHashCode。 All works fine until the variable is changed within the Game class. 一切正常,直到在Game类中更改了变量。 Then the returned identityHashCode starts to jump fore and back between the new and the old object. 然后,返回的identityHashCode开始在新对象和旧对象之间来回跳转。 Thats a behavior I have never seen before and hoped you guys had any advice or even hint on whats happening. 那是我从未见过的行为,希望你们对发生的事情有任何建议甚至暗示。

Thanks in advance, I attach part of the source code and the console output. 在此先感谢,我附加了部分源代码和控制台输出。

public GamePlayState(int sid) {
            stateID = sid;
            entityManager = StateBasedEntityManager.getInstance();
            game = new Game(this);
            //TEST CODE ONLY
            Thread t = new Thread(this);
            t.start();
    }

    /******TEST CODE ONLY******/
    public synchronized void run () {
            while (true) {
                    System.err.println(System.identityHashCode(game.getShootL()));
                    try {
                            wait(500);
                    } catch (InterruptedException e) {
                            e.printStackTrace();
                    }
            }
    }

Output 产量

611581887 611581887
611581887 611581887
611581887 611581887
611581887 611581887
611581887 611581887
611581887 611581887
Now value is changed 现在值已更改
1709366259 1709366259
611581887 611581887
1709366259 1709366259
611581887 611581887
1709366259 1709366259
611581887 611581887
1709366259 1709366259

We were unable to find a real answer so far but making the variable returned by the getter Method static eliminated the problem. 到目前为止,我们还没有找到真正的答案,但是将getter方法返回的变量设为静态可以消除该问题。 Looks like the underlaying 3rd party engine is messing with the objects variables!? 似乎底层的3rd Party引擎正在混乱对象变量!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM