Separate update and render
- by NSAddict
I'm programming a simple Snake in Java. I'm a complete newbie when it comes to Java and Game Developing, so please bear with me ;)
Until now, I have been using a UI thread, as well as a update-thread.
The update thread just set the position, set the GameObjects, and so on.
I didn't think much of concurrency, but now I've come to a problem.
I wanted to modify the ArrayList<GameObject>, but it throws a java.util.ConcurrentModificationException.
With a little research I found out that this happens because the two threads are trying to access the variables at the same time.
But I didn't really find a way to prevent this.
I thought about copying the array and swapping them out when the rendering is finished, but I would have to deep-copy them, which isn't really the best solution in my opinion.
It probably eats up more CPU resources than a single-threaded game.
Are there any other ways to prevent this?
Thanks a lot for your help!