Class ThreadResourcePool
In: lib/thread_resource_pool.rb
Parent: Object

Copyright (c) 2007 FlexiGuided GmbH, Berlin

Author: Jan Behrens

Website: www.flexiguided.de/publications.flexirecord.en.html


ThreadResourcePool‘s are pools of resources to be exclusively used by each thread of an application. They are abstract classes, and can‘t be used directly. You have to create sub-classes, which implement the following three methods:

Methods

Included Modules

MonitorMixin

Public Class methods

Creates a new pool with a given cache-size. If no cache-size is given, then a size of 1 is assumed.

Public Instance methods

Returns the resource being associated with the current thread, or nil there is none existing.

Checks if the current thread has already an associated resource, and either yields that resource or a newly generated or cached resource to the given block. If the thread had no resource associated at the time of call, the resource will be put back to the cache or destroyed when the block ends.

Private Instance methods

Sets (or deletes, when being passed nil) the resource being associated with the current thread.

Destroys a resource. This method is to be implemented by a sub-class.

Returns a new resource to use. This method is to be implemented by a sub-class.

Returns a new resource to use for the current thread. It is taken from the cache, or, if the cache is empty, by calling the generate_resource method, which has to be implemented by a child class of TheadResourcePool.

Puts a used resource back to the cache (or destroys it, if the cache is full). Before it is put back to the resource cache, the reset_resource method is called, which has to be implemented by a child class of ThreadResourcePool. Only if that method returns true (or any non nil/false value) the resource is used again, otherwise it is destroyed. Destruction has to be implemented as the method destroy_resource in a child class of ThreadResourcePool.

Resets a resource to be re-used by another thread. Returns true on success or false if the resource is to be destroyed. This method is to be implemented by a sub-class.

[Validate]