I'm using libgdx for a game I'm writing, where I have another thread that needs to update the graphics thread. I was getting crashes as libgdx is not thread safe (intentionally) and I was allowing the other thread to directly modify a variable in the graphics thread.
The libgdx docs suggest something similar to the following code. It's basically a closure containing the incoming information, which is then processed when the graphics thread gets to it.
I've modified it to declare the runnable outside of the the listener in the hopes that I could avoid garbage collection, but it occurs to me that I may have created a race condition now where runnable could be overwritten prior to the graphics thread consuming the previous information?
So far I've been able to avoid garbage collection everywhere else, and my game is also utilizing the low-latency audio bindings in Android so garbage collection is really my enemy.
Any suggestions?
private Runnable runnable;
private SomeListener listener = new SomeListener() {
@Override
public void messageIn(final String source, final String s, final Object... l) {
runnable = new Runnable() {
@Override
public void run() { getWorkspace().messageIn(s,l); }
};
Gdx.app.postRunnable(runnable);
}
};