So, the game works fine, but I noticed that in extremely rare cases the application crashes in random places. I did my debugging, and my theory is that since touch events on the surface are processed in the user interface thread of the application, and games are updated in a separate thread (the thread of the loop or what it is called), these two step on top of each other. My solution was to synchronize surface touch events:
@Override public boolean onTouchEvent(MotionEvent event) { synchronized (this.gameLoopingThread) {
with updating the game in the loop thread:
@Override public void run() { while (this.running) { synchronized (this) { doUpdate(); } doDraw(); } }
So I wonder if I am okay to do this? I am not very good at multi-threaded applications, so someone will tell me if this solves my problem, because I cannot check, because the problem is not often enough (this happened only twice). And if so, shouldn't we do this in every game we play? Textbooks that I have seen everywhere on the Internet never do this. Is there a reason?
source share