Worker Pools in ActionScript

Our example demonstrates how to implement a worker pool using threads and queues in ActionScript.

package {
    import flash.utils.setTimeout;
    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class WorkerPools extends EventDispatcher {
        private const NUM_WORKERS:int = 3;
        private const NUM_JOBS:int = 5;
        
        private var jobs:Array = [];
        private var results:Array = [];
        private var workerCount:int = 0;

        public function WorkerPools() {
            for (var j:int = 1; j <= NUM_JOBS; j++) {
                jobs.push(j);
            }

            for (var w:int = 1; w <= NUM_WORKERS; w++) {
                startWorker(w);
            }
        }

        private function startWorker(id:int):void {
            workerCount++;
            var worker:Worker = new Worker(id, this);
            worker.addEventListener(WorkerEvent.JOB_COMPLETE, onJobComplete);
            worker.start();
        }

        private function onJobComplete(event:WorkerEvent):void {
            results.push(event.result);
            trace("Job complete. Results: " + results.length);

            if (results.length == NUM_JOBS) {
                dispatchEvent(new Event(Event.COMPLETE));
            }
        }

        public function getJob():int {
            return jobs.length > 0 ? jobs.shift() : -1;
        }
    }
}

import flash.events.EventDispatcher;
import flash.events.Event;

class Worker extends EventDispatcher {
    private var id:int;
    private var pool:WorkerPools;

    public function Worker(id:int, pool:WorkerPools) {
        this.id = id;
        this.pool = pool;
    }

    public function start():void {
        processNextJob();
    }

    private function processNextJob():void {
        var job:int = pool.getJob();
        if (job == -1) return;

        trace("worker " + id + " started  job " + job);
        
        setTimeout(function():void {
            trace("worker " + id + " finished job " + job);
            dispatchEvent(new WorkerEvent(WorkerEvent.JOB_COMPLETE, job * 2));
            processNextJob();
        }, 1000);
    }
}

import flash.events.Event;

class WorkerEvent extends Event {
    public static const JOB_COMPLETE:String = "jobComplete";
    public var result:int;

    public function WorkerEvent(type:String, result:int) {
        super(type);
        this.result = result;
    }
}

This ActionScript implementation simulates a worker pool using the following approach:

  1. We create a WorkerPools class that manages the jobs and results.
  2. The Worker class represents individual workers that process jobs.
  3. We use setTimeout to simulate the delay of processing a job.
  4. Events are used to communicate job completion back to the pool.

To use this worker pool:

var pool:WorkerPools = new WorkerPools();
pool.addEventListener(Event.COMPLETE, function(event:Event):void {
    trace("All jobs completed");
});

When run, you’ll see output similar to this:

worker 1 started  job 1
worker 2 started  job 2
worker 3 started  job 3
worker 1 finished job 1
worker 1 started  job 4
worker 2 finished job 2
worker 2 started  job 5
worker 3 finished job 3
worker 1 finished job 4
worker 2 finished job 5
All jobs completed

This example demonstrates concurrent processing in ActionScript, simulating the behavior of worker pools. While ActionScript doesn’t have native support for true multithreading, this approach using asynchronous operations and event-driven programming can achieve similar results in terms of managing multiple tasks concurrently.