Search code examples
javalinked-listdelaylag

LinkedList produces a huge delay on add()


I've been working on a little proof of concept game that has a rolling environment in the background (2D). I have a custom class object called roadTile, which basically is a 200px tall block with a picture to paint on and some physics. I'm storing the tiles in a LinkedList 5 at a time. I have a main loop that is responsible for moving everything and checking for collisions. The loop is supposed to be performed every 50ms (controlled by a timer) and it keeps the rate fixed by measuring the length of the last run and then taking deducting it from the sleep-time.

This usually runs fine and the loops run in less than a millisecond, but when I start the program, it "chokes" after the first 2-7 times. I removed the old tile and put a new one on the bottom of the list. On calling the add (new roadTile()), the program halts for 20-400ms which is a millenium in computer time, and to top this off, the behavior isn't consistent. Sometimes it works fine some times not.

I'm pretty clueless how to eliminate this, any thoughts?


Solution

  • Make sure that you cache everything before you start working with graphics. This might be the cause of your delay.