user978758
user978758

Reputation: 599

Multithreads issue

I have an aplicaton In libgdx with two threads. In one thread the method generate numbers (it works only if I press buton, I use Gdx.graphics.setContinuousRendering(false); to stop it)

public int numbersShow(){
    List<String> d = generateNumbers.generate();
     a = d.get(0);
     b = d.get(1);
     c = d.get(2);
     e = d.get(3);
     f = d.get(4);
     pos1 = (Integer) p.get(0);
     pos2 = (Integer) p.get(1);
     pos3 = (Integer) p.get(2);

    if(pos1==0){
        number.draw(batch, e, 50, 350);
     position=0;
    }
    if(pos1==1){
        number.draw(batch, f, 50, 350);
        position=1;
    }
    if(pos1==2){
        number.draw(batch, c, 50, 350);
        position=2;
    }
         return position;
}

The second method run all the time (In runnable class) and I want to take „position” variable:

@Override
public void run() {
    Gdx.graphics.requestRendering();
    numbersGame = new NumbersGame().position;
    if(Gdx.input.isKeyPressed(Input.Keys.NUM_1)){
        Gdx.app.log("pressed", "1");
     p1 = numbersGame;
        Gdx.app.log("p1", ""+p1);

    }

But all the time the p1 = 0 (but numbersShow() method generates different values 0,1 ,2…). I don’t know where I make mistake. Thanks for help.

Upvotes: 1

Views: 79

Answers (1)

TwoThe
TwoThe

Reputation: 14269

numbersGame = new NumbersGame().position; will get the value of position during creation of the Object. If you call numbersShow() at a later time, it will not retroactively change the value of numbersGame.

This a value copy and not a reference copy.

Upvotes: 1

Related Questions