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
Please visit http://www.baynewmedia.com/ or email con@baynewm edia.com for information, questions, concerns, or bug reports. tact
__/ 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.
ThreadManager | Manages 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 | |
ThreadManager | Constructor method for the class. |
createThread | Creates a Thread instance and assigns all of the associated parameters. |
startThreads | Begins threaded execution of any threads added via the createThread method. |
stopThreads | Stops any executing threads. |
destroyThread | Destroys (removes) a running thread from the execution stack. |
executeThreads | Executes threads based on the provided execution model. |
findThread | Finds a thread from the execution stack based on the supplied Thread object instance. |
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.
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).
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.
ThreadManager.ONADDTHREAD Error.THREADMANAGER
nothing
no
public function startThreads( ... args ):void
Begins threaded execution of any threads added via the createThread method.
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.
nothing
ThreadManager.ONSTARTTHREADS
nothing
no
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.
eventObj (TimerEvent): A standard system Timer event.
nothing
nothing
nothing
no
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.
threadObj (Thread, required): The Thread instance to find.
Object: Containing the “priority” (uint) and “index” (uint) of the found Thread, or NULL is no matching thread was found.
nothing
nothing
(Timer) Execution Timer instance.
private static var ThreadManager_timer: Timer
(Array) An array of all created threads.
private static var ThreadManager_threads: Array
(Object) Tracks the current execution state including the current Thread offset, current priority, current execution style, and so on.
private static var ThreadManager_executionTrack: Object
Constructor method for the class.
public function ThreadManager ()
Creates a Thread instance and assigns all of the associated parameters.
public function createThread( threadObj: Object, ... parameters ):Thread
Begins threaded execution of any threads added via the createThread method.
public function startThreads( ... args ):void
Stops any executing threads.
public function stopThreads()
Destroys (removes) a running thread from the execution stack.
public function destroyThread( threadObj: Thread )
Executes threads based on the provided execution model.
private function executeThreads( eventObj: TimerEvent ):void
Finds a thread from the execution stack based on the supplied Thread object instance.
private function findThread( threadObj: Thread )