Because you are calling tick()
in another thread, you have entered the dark world of concurrency and thread safety.
Put simply, changes made in one thread are not necessarily visible to other threads, unless you follow some strict rules:
- make the field
volatile
- all access to it must be synchronized
Ergo:
private volatile int timer = 0;
public synchronized void tick(){
if(updateVar == true) timer++;
}
pubic synchronized int getTimer(){
return timer;
}
You may also have to join()
the updating thread to wait for it to complete if you are not in a wait loop in the main thread.