Take it as thread

Just a short missive this time, to mention that I’ve sped up my “world’s dumbest Agent-Based Model” by a factor of around fifty by having the calculation of the model and the display of the model (using Processing) happen in different threads. It’s dead easy to do in Java and the benefits are mind-blowing.

I can now simulate 250 agents in a world 500×500 spaces big in about 5 seconds. That’s pretty good, I reckon.

Here’s how it works:

public class Game implements Runnable {
  public void run() {
    do { /* run the game */ } while (true);
  }
}

and meanwhile, elsewhere, Processing does this:

setup() {
  new Thread(Game).start();
}

draw() {
  // draw the game whenever Processing
  // actually gets round to it!
}

It’s embarrassingly easy with Java and, as I’ve said, the benefits are astounding.