ThreadManager

Manages the creation, destruction, and execution of thread instances.  Threads are a safe way of executing looped code that may require to run indefinitely.  The Thread Manager uses a Timer object to execute threads but rather than executing them whenever a new timer tick becomes available, the Thread Manager executed them in sequence and based on priority.  As many as 5000 threads may be executed per timer tick

__/ REQUIREMENTS \__

ActionScript 3.0 (or later)

__/ IMPORTS \__

com.bnm.api.instances.ErrorBroadcast com.bnm.api.instances.Thread com.bnm.api.instances.Exception flash.utils.* flash.events.* flash.system.*

__/ EXTENDS \__

com.bnm.api.Events

__/ IMPLEMENTS \__

nothing

__/ NOTICE \__

©opyright 2008 Bay New Media.  All rights, as stipulated in the The MIT License, are reserved.

This ActionScript library is licensed and released under the MIT License (http://www.opensource.org/licenses/mit-license.php).  Please refer to the accompanying text file “license.txt”, which should be distributed with every package and version of this library, for information on usage, distribution, copying, and other important information.

Please visit http://www.baynewmedia.com/ or email con.nosp@m.tact@baynewm.nosp@m.edia.com for information, questions, concerns, or bug reports.

__/ NOTES \__

This class is commented using the NaturalDocs documentation system.  It is intended to be easily read both from the direct source code and from an adaptation that is generated as hyperlinked HTML.  The NaturalDocs specification used in this class’ documentation is version 1.35 though older versions may probably be used safely.  For more information, please visit: http://www.naturaldocs.org/

The BNMAPI is officially available (primary sources) on Source Forge (http://sourceforge.net/projects/bnmapi/), on the Bay New Media site (http;//www.baynewmedia.com/), on the PeaBee project site (http://www.peabee.com/), and through the Bay New Media SVM repository server (visit http://www.baynewmedia.com/ for more information).  Downloads from other sources may not be complete or up-to-date.

If you’re interested in joining the BNMAPI project in any capacity (development, testing, distribution, etc.), please visit us on Source Forge or the Bay New Media web site.

Summary
ThreadManagerManages the creation, destruction, and execution of thread instances.
Variables
ThreadManager_timer(Timer) Execution Timer instance.
ThreadManager_threads(Array) An array of all created threads.
ThreadManager_executionTrack(Object) Tracks the current execution state including the current Thread offset, current priority, current execution style, and so on.
Functions
ThreadManagerConstructor method for the class.
createThreadCreates a Thread instance and assigns all of the associated parameters.
startThreadsBegins threaded execution of any threads added via the createThread method.
stopThreadsStops any executing threads.
destroyThreadDestroys (removes) a running thread from the execution stack.
executeThreadsExecutes threads based on the provided execution model.
findThreadFinds a thread from the execution stack based on the supplied Thread object instance.

Variables

ThreadManager_timer

private static var ThreadManager_timer: Timer

(Timer) Execution Timer instance.  Used for core thread execution timing.

ThreadManager_threads

private static var ThreadManager_threads: Array

(Array) An array of all created threads.  Not all these threads should be assumed to be running.

ThreadManager_executionTrack

private static var ThreadManager_executionTrack: Object

(Object) Tracks the current execution state including the current Thread offset, current priority, current execution style, and so on.

Functions

ThreadManager

public function ThreadManager ()

Constructor method for the class.

Parameters

none

Returns

ThreadManager: An instance of the ThreadManager class.

Throws

nothing

Broadcasts

nothing

Bubbles

no

See also

createThread

public function createThread(threadObj: Object,
... parameters):Thread

Creates a Thread instance and assigns all of the associated parameters.  This does not actually start threaded execution, this is done by calling startThreads.

Parameters

threadObj (Object): A generic object containing the properties to assign to the thread instance.  Where possible, default values will be assigned.  Possible properties include: method (Function, required): A reference to the method to be called on each thread iteration. context (untype, optional): The context, or scope, in which the method should be executed.  This is usually “this” but may be set to another object reference to execute in that scope instead. priority (uint, optional): The execution priority to assign to the thread.  The higher the priority, the higher the call will be on the stack.  In other words, a priority of 1 will be executed before a priority of 2.  If not specified, the default priority is 50.  Minumum priority is 0, maximum is 100. iterations (uint, optional): The number of iterations that the thread should execute before the thread removes itself.  This is 1-based so that a value of 1 will execute exactly once.  Values less than 1 will execute indefinitely.  Default is 0. timeObject (TimeObject, optional): Defines the execution timer for each thread tick.  Defining a 1 second TimerObject, for example, will create a thread that executes every second.  A TimeObject is used for its self-sufficient functionality.  ... parameters (untyped, optional): Any parameters you want to pass to your threaded method should appear here, in order.  These will be passed exactly as specified.  Any variables that need to change on each execution loop (an offset counter, for example) need to be placed inside objects otherwise only the value will be passed and cannot be updated (it is discarded after the method is invoked).

Returns

Thread: A new instance of a Thread object.  This thread should not yet be assumed to be running.  If a thread could not be created (for example, if the parameters were incorrect), NULL is returned.

Broadcasts

ThreadManager.ONADDTHREAD Error.THREADMANAGER

Throws

nothing

Bubbles

no

startThreads

public function startThreads(... args):void

Begins threaded execution of any threads added via the createThread method.

Parameters

threadSpeed (unint, optional): The execution speed at which to run the threaded stack at, in milliseconds.  Default is 1. executionStyle (String, optional): Defines how threads are to be executed.  “stacked” will execute a subsequent thread on each timer tick.  This is safer since more execution time is allotted per tick, but also slower.  “all” (default) will execute all threads on each timer tick.  This is much faster but can also cause severe execution lag if threaded functions need to execute complex code.

Returns

nothing

Broadcasts

ThreadManager.ONSTARTTHREADS

Throws

nothing

Bubbles

no

stopThreads

public function stopThreads()

Stops any executing threads.  This does not remove the threads from the stack, however, it only stops their execution.

Parameters

none

Returns

nothing

Broadcasts

ThreadManager.ONSTOPTHREADS

Throws

nothing

Bubbles

no

destroyThread

public function destroyThread(threadObj: Thread)

Destroys (removes) a running thread from the execution stack.  This does not stop threaded execution, only the one thread instance is affected.

Parameters

none

Broadcasts

nothing

Throws

nothing

Bubbles

no

executeThreads

private function executeThreads(eventObj: TimerEvent):void

Executes threads based on the provided execution model.  This method automatically removes threads when they have completed and resets the thread stack when no threads are active.

Parameters

eventObj (TimerEvent): A standard system Timer event.

Returns

nothing

Broadcasts

nothing

Throws

nothing

Bubbles

no

findThread

private function findThread(threadObj: Thread)

Finds a thread from the execution stack based on the supplied Thread object instance.  The returned value is an object containing the thread’s priority and execution stack index.

Parameters

threadObj (Thread, required): The Thread instance to find.

Returns

Object: Containing the “priority” (uint) and “index” (uint) of the found Thread, or NULL is no matching thread was found.

Broadcasts

nothing

Throws

nothing

The Events class is the heart of the BNMAPI.
Manages the tracking, progress, and reporting for a single binary/data load.
Manages binary/data loads for the API.
private static var ThreadManager_timer: Timer
(Timer) Execution Timer instance.
private static var ThreadManager_threads: Array
(Array) An array of all created threads.
private static var ThreadManager_executionTrack: Object
(Object) Tracks the current execution state including the current Thread offset, current priority, current execution style, and so on.
public function ThreadManager ()
Constructor method for the class.
public function createThread(threadObj: Object,
... parameters):Thread
Creates a Thread instance and assigns all of the associated parameters.
public function startThreads(... args):void
Begins threaded execution of any threads added via the createThread method.
public function stopThreads()
Stops any executing threads.
public function destroyThread(threadObj: Thread)
Destroys (removes) a running thread from the execution stack.
private function executeThreads(eventObj: TimerEvent):void
Executes threads based on the provided execution model.
private function findThread(threadObj: Thread)
Finds a thread from the execution stack based on the supplied Thread object instance.