Concurrency / Threading with Flash & Actionscript 3

Update: Adobe Flex and the Flash platform have been retired.

NOTE: Please click here for a gwt/javascript version of this technique.

Flash applications do not support concurrency. All code must be executed within a single thread. This means that any large calculations or processor intensive tasks will cause the flash player to stall very quickly. We have to rely on server side operations or other technologies to perform large calculations.

Fortunately it is possible to simulate threading using the flash platform. The flex example below demonstrates psuedo-threading using actionscript 3. The sample application increments an integer from zero to one hundred million, displaying the count on the screen as it increments. If this code was executed in a for loop–or even a frame loop–our application would quickly become unresponsive.


In order to mitigate this problem we can configure our application to stop incrementing the integer whenever our application needs to redraw the stage. This allows our application to catch it’s breath whenever necesary.

As a consequence our procedure needs to be configured in such a way that it can be paused and resumed once the screen has refreshed. This means our large calculations need to be broken down into smaller calculations.

Press the ‘Start’ button to demo the application.

Update: Adobe Flex and the Flash platform have been retired.

Source for the sample flex application can be downloaded here. The actionscript is explained below:

public var count:int = 0;
		
public function init():void
{
    this.startBtn.addEventListener(MouseEvent.CLICK,start);
}

private function start(event:MouseEvent):void
{
    this.startBtn.removeEventListener(MouseEvent.CLICK,start);
    this.startBtn.enabled = false;
    
    var thread:PseudoThread = new PseudoThread(systemManager, this.bigComputation, { max:100000000 } );
    this.addEventListener(Event.ENTER_FRAME,this.onDraw);
}

private function bigComputation(obj:Object):Boolean
{
    var result:Boolean = true;
    
    if ( obj.max > this.count )
        this.count++;
    else
        result = false;
    
    return result;
}

private function onDraw(event:Event):void
{
    this.labelField.text = this.count+"";
}

The line:

public var count:int = 0;

creates an integer variable. This variable will be incremented to one hundred million and referened throughout the application.

public function init():void
{
    this.startBtn.addEventListener(MouseEvent.CLICK,start);
}

This function is called onc the application has loaded and adds a listener to our ‘Start’ button

private function start(event:MouseEvent):void
{
    this.startBtn.removeEventListener(MouseEvent.CLICK,start);
    this.startBtn.enabled = false;
    
    var thread:PseudoThread = new PseudoThread(systemManager, this.bigComputation, { max:100000000 } );
    this.addEventListener(Event.ENTER_FRAME,this.onDraw);
}

This method is called once our ‘Start’ button has been pressed. Firstly we create an instance of PseudoThread. PseudoThread is a class created by Alex Harui. When instantiating a PseudoThread object we need to pass three arguments. ‘systemManager’ which is a reference to the applications internal system manager. This is used to enable the class to determine when the screen needs to be refreshed. ‘this.bigcomputation’ is a reference to another method in our application. This method needs to return a boolean value.It must return false if the calculation has not finished and true once it is done. ‘{ max:100000000 }’, our third argument is an object, the object is passed to the bigcomputation method as an argument. In this case we are indicating the maximum value we wish to increment our integer to. lastly we add an ENTER_FRAME loop ehich will update the value of our integer on the screen.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *