During this week I refactored titiritero and in particular the implementation of the gameloop. The gameloop is a control structure that runs the simulation (give live to model) and updates the views. There are to ways of implementing it:
The first one, is with an infinite loop with a Thread.sleep inside, something like this:
shouldExecuteGameloop = true; while(shouldExecuteGameloop ){ this.runSimulation(); this.updateView(); Thread.sleep(simulationInterval); }
The other alternative is using a Timer object and making the gameloop class to implement TimerTask interface, something like this:
Timer timer = new Timer(gameloop); timer.start(simulationInterval); ... gameloop.onTimerTask(){ this.runSimulation(); this.updateView(); }
I choose the first alternative because with the second one it could happen that the execution of a single loop of the gameloop takes to much time, so there could be more than one thread working simultaneously on the same object, producing an anomalous behaviors of the application.