Is getTick()
thread safe? I.e. is there a synchronized key word, a lock of some form, a volatile read or an Atomic variable on the way to the tick value? If not, your thread may not see concurrent changes made to the tick counter. Breaking and debugging may force these changes to be visible.
Additionally it seems like your producer should not wait for a successful queue.put()
, but for the application to advance to the next tick. Is myWorld.myApp.getTick()
blocking? If not, your code will just loop endlessly, unless :
- There are other threads modifying the tick concurrently
- Your thread is suspended and another awoken thread modifies the tick
Using an endless loop to check on changes of a condition (aka polling/spin waiting) only makes sense, if there is the possibility of changes.
I would suggest to to implement a blocking and safely publicating waitForTick(int tick)
in the object myWorld.myApp
. Apart from thread safe collections there are other concurrent components like Semaphores, Barriers etc. that can be used to synchronize different threads.