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.

Advertisements

Author: Rob Levy

Economist at NEF. Former teaching Fellow in Economics at UCL and Bristol University. Recently submitted my PhD. We'll see what happens...

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s