I've been working on implementing a software threading framework for as3. The idea, since we're constrained to a single thread for our ActionScript, is to break apart processes, and allow them to be run in the background; ideally without disrupting the game/application/whatever.
The 2 things that first came to mind are what are in the demos. First was encoding a large (or many, or both!) images and the second was pathfinding. Both of these can totally lock up the player. You can see that in both cases the UI will totally freeze when executing linearly in one go on one frame, whereas the (admittedly slower) "green" thread method, does a fair job of keeping things responsive.
image encoding demo: http://lab.generalrelativity.org/thr...imageEncoding/
pathfinding demo:
http://lab.generalrelativity.org/threading/pathfinding/
In the image encoding demo, I'm encoding that same image 3 times in both cases. You can see, with the little diagnostic graphs, allocation opening up in the thread as other processes complete.
The API is pretty straightforward, you implement a simple interface that allows your process (usually a chunk of an algorithm) to be run. Then you create a thread and pass it a list of all processes to execute, a frequency and a percentage share of the processor. The thread will do its best to maintain the framerate by allocating only the constrained amount of time and policing misbehaving processes. Threads can yield to allow for other heavy-processing to take precedence and then pick back up right where they left off.
A verbose use of the framework would be:
But there are also utility methods to shorthand some of this:Code://create the processes to execute
var process1:IRunnable = new SomeProcess();
var process2:IRunnable = new SomeOtherProcess();
//wrap them in an array to pass to the thread
var processes:Vector.<IRunnable> = Vector.<IRunnable>( [ process1, process2 ] );
//define frequency
var hertz:Number = stage.frameRate;
//allow the thread to occupy whatever percentage of player's processing
var share:Number = 0.3;
//create the thread
var thread:GreenThread = new GreenThread( processes, hertz, share );
//listen for process and thread complete events
thread.addEventListener( GreenThreadEvent.PROCESS_COMPLETE, processCompleteHandler );
thread.addEventListener( Event.COMPLETE, threadCompleteHandler );
//open the thread
thread.open();
I have some more work on it still, but will be releasing it shortly to anyone interested.Code:ThreadUtil.openSingle( myProcess, callbackMethod );