Can ’expensive code’ be put in an asyncronous background proccess/thread?

Hi all,

I have kind of a broad question, and I need a bit of guidance as to where I should look or what to explore. Surely there is a name for this issue I can Google, or a design pattern I can adapt.

My problem: I have some very ‘expensive’ code that takes a long time to run… loops within loops within loops … for things like terrain generation and path finding. I can’t run this code and cache the results before my game starts.

My question: Where is the best place to execute this code so it doesn’t effect frame rate? Is there an asynchronous process or thread where I can put it ? When I put it in the update loop (in Input handling or Application States) it obviously freezes the game while executing.

An example: My massive game ‘map’ is divided into smaller sub maps like squares on a chess board, only 1 sub map is visible at a time. when I need to move the the next sub map, I first need to generate it, then draw it on screen. While generating, the game freezes for a few seconds. I would like to start generating the new sub map over a few seconds before I need it, so its ready to drop in when required.

The only other solution I can think of is to rip my loops (that iterate 10,000 times) apart into a method that iterate say only 10 items at a time, and then call then call that method once every update loop.

Thanks for your time


EDIT: JME3 if that makes any difference :slight_smile:

You can use separate threads, as long as you don’t alter any objects in the “live” scene graph that are attached to the rootNode, guiNode, etc.

I have put up some hints at how to multithread in jme3:

Most important thing is the thread safe access of the scenegraph, yes.

perfect, thanks guys.