简体   繁体   中英

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. All works fine until the variable is changed within the Game class. Then the returned identityHashCode starts to jump fore and back between the new and the old object. 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
Now value is changed
1709366259
611581887
1709366259
611581887
1709366259
611581887
1709366259

We were unable to find a real answer so far but making the variable returned by the getter Method static eliminated the problem. Looks like the underlaying 3rd party engine is messing with the objects variables!?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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