| Package | vegas.utils.pool |
| Class | public class ObjectPool |
| Inheritance | ObjectPool Object |
| Property | Defined By | ||
|---|---|---|---|
| builder : ObjectPoolBuilder
Defines the builder responsible for creating all pool objects. | ObjectPool | ||
| growing : Boolean
Indicates if the pool of objects is auto growing when a new user is called with the "object" property. | ObjectPool | ||
| length : int [read-only]
Indicates the pool size. | ObjectPool | ||
| parameters : Array
The optional Array representation of parameters to send in the ObjectPoolBuilder.build() method use in the pool to create all objects. | ObjectPool | ||
| usageCount : int [read-only]
Indicates the total number of 'checked out' objects currently in use. | ObjectPool | ||
| wasteCount : int [read-only]
The total number of unused thus wasted objects. | ObjectPool | ||
| Method | Defined By | ||
|---|---|---|---|
ObjectPool(growing:Boolean = false)
Creates a new ObjectPool instance. | ObjectPool | ||
allocate(size:uint = 1, clazz:Class = null, parameters:Array = null):void
Allocates the pool by creating all objects from the builder. | ObjectPool | ||
destroy():void
Destroy and unlock all ressources for the garbage collector. | ObjectPool | ||
dispose(o:*):void
Puts it back for the next use the specified object. | ObjectPool | ||
flush():void
Removes all unused objects from the pool. | ObjectPool | ||
get():*
Returns the next available object from the pool. | ObjectPool | ||
initialize(name:String, args:Array):void
Helper method for applying a function to all objects in the pool. | ObjectPool | ||
| builder | property |
public var builder:ObjectPoolBuilderDefines the builder responsible for creating all pool objects. If you don't want to use a factory, you must provide a class to the allocate method instead.
See also
| growing | property |
public var growing:BooleanIndicates if the pool of objects is auto growing when a new user is called with the "object" property.
| length | property |
length:int [read-only] Indicates the pool size.
public function get length():int| parameters | property |
public var parameters:ArrayThe optional Array representation of parameters to send in the ObjectPoolBuilder.build() method use in the pool to create all objects.
| usageCount | property |
usageCount:int [read-only] Indicates the total number of 'checked out' objects currently in use.
public function get usageCount():int| wasteCount | property |
wasteCount:int [read-only] The total number of unused thus wasted objects. Use the purge() method to compact the pool.
public function get wasteCount():intSee also
| ObjectPool | () | Constructor |
public function ObjectPool(growing:Boolean = false)Creates a new ObjectPool instance.
Parametersgrowing:Boolean (default = false) — Indicates if the pool of objects is auto growing when a new user is called with the "object" property.
|
| allocate | () | method |
public function allocate(size:uint = 1, clazz:Class = null, parameters:Array = null):voidAllocates the pool by creating all objects from the builder.
Parameters
size:uint (default = 1) — The class to create for each object node in the pool.
| |
clazz:Class (default = null) — The number of objects to create.
| |
parameters:Array (default = null) — The optional Array representation of parameters to send in the ObjectPoolBuilder.build() method use in this method to create all pooling objects.
This overwrites the current factory.
|
| destroy | () | method |
public function destroy():voidDestroy and unlock all ressources for the garbage collector.
| dispose | () | method |
public function dispose(o:*):voidPuts it back for the next use the specified object.
Parameters
o:* |
| flush | () | method |
public function flush():voidRemoves all unused objects from the pool. If the number of remaining used objects is smaller than the initial capacity defined by the allocate() method, new objects are created to refill the pool.
| get | () | method |
public function get():*Returns the next available object from the pool.
Returns* |
Error — If the pool is empty and resizable, an error is thrown.
|
| initialize | () | method |
public function initialize(name:String, args:Array):voidHelper method for applying a function to all objects in the pool.
Parameters
name:String — The name of the method invoked to initialize all objects in the pool.
| |
args:Array — The Array representation of all arguments of the init method.
|
import vegas.util.pool.ObjectPool ;
import examples.pool.MyBuilder ;
import examples.pool.MyClass ;
var i:int;
var pool:ObjectPool = new ObjectPool() ;
pool.allocate( 10 , MyClass , ["hello label"] ) ;
pool.initialize("init", ["arg1", "arg2"]) ;
var activeObjects:Array = [] ;
//read the first object
activeObjects[0] = pool.get() ;
var k:int = pool.wasteCount ;
trace("pool.wasteCount : " + pool.wasteCount) ; // 9
for ( i = 0 ; i < k ; i++ )
{
activeObjects.push( pool.get() ) ; // read the remaining 9 objects
}
// wasteCount is now zero, but usageCount reports 10.
trace("pool.usageCount : " + pool.usageCount) ;
try
{
//this will fail because the pool is now empty
activeObjects.push( pool.get() );
}
catch (e:Error)
{
trace(e);
}
k = pool.size ;
// give all objects back to the pool
for (i = 0; i < k; i++)
{
pool.dispose( activeObjects.shift() ) ;
}
// usage count is zero
trace( pool.usageCount ) ;
trace( "======= use grow property" ) ;
pool.grow = true ;
pool.get() ; // create a new object and the pool is growing witn 10 new objects inside the buffer.
trace("pool.usageCount : " + pool.usageCount) ; // 1
trace("pool.wasteCount : " + pool.wasteCount) ; // 9
trace( "======= use destroy method" ) ;
pool.destroy() ;
trace("pool.usageCount : " + pool.usageCount) ; // 0
trace("pool.wasteCount : " + pool.wasteCount) ; // 0
trace( "======= Assign a custom object factory, see the test.pool.MyBuilder class") ;
pool.builder = new MyBuilder();
pool.allocate( 20 ) ;
trace( pool.object ) ;
trace("pool.usageCount : " + pool.usageCount) ; // 1
trace("pool.wasteCount : " + pool.wasteCount) ; // 19