title: Thread game loop causing problem with ArrayList
body: <p>This is my 1st question on StackOverflow. I'm not a native English speaker so sorry for any mistakes.</p>
  
  <p>FACTS:<br>
  I'm making 2D game in Android studio using Java. I'm using <code>SurfaceView</code>, abstract class <code>GameObject</code> and <code>ArrayList&lt;GameObject&gt;</code>. To loop through ArrayList I'm mostly using for-each loop - <code>for(GameObject obj: ArrayList)</code>. As a game loop I was using simple 60 fps game loop, but my computer emulator could reach max about 35 fps only.</p>
  
  <p>PROBLEMS:<br>
  I have been working on this for couple of month and now i decided to use better game loop to have fixed update per second speed, so it will run the same speed on emulator and on devices. As soon as I try to <code>update()</code> more frequently then <code>draw()</code>, game started crashing on <code>for(GameObject obj: ArrayList)</code>.</p>
  
  <blockquote>
    <p>E/AndroidRuntime: FATAL EXCEPTION: Thread-2<br>
        Process: com.janomat.helicosquares, PID: 27202<br>
        java.util.ConcurrentModificationException<br>
            at java.util.ArrayList$Itr.next(ArrayList.java:860)<br>
            at com.janomat.helicosquares.GameView.update(GameView.java:536)<br>
            at com.janomat.helicosquares.GameThread.run(GameThread.java:155)</p>
  </blockquote>
  
  <p>Long time ago when I was learning to code in Java I found out that using for-each loop through ArrayList can make a problem if I need to remove obj. So I changed for-each loop for classic <code>for(int a=1; a &lt; ArrayList.size; a++)</code> but then it stops rendering objects from array list. It looks like the objects are on screen invisible. I tried few game loops, even I made one but still the same.</p>
  
  <p>Any idea what I'm doing wrong?</p>