Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

The ObjectPool Class

c



+ Font mai mare | - Font mai mic



The ObjectPool Class

Let's start our discussion of the ObjectPool class by listing it in its entirety:

using System;
using System.Collections;
using System.Timers;

namespace WroxCS


internal ObjectPool()


protected abstract object Create();

protected abstract bool Validate(object o);

protected abstract void Expire(object o);

internal object GetObjectFromPool()

else


} catch (Exception)
o = Create();
locked.Add(o, now);

return(o);


internal void ReturnObjectToPool(object o)




private void CollectGarbage(object sender,
System.Timers.ElapsedEventArgs ea)



catch (Exception)





The ObjectPool base class contains two important methods; GetObjectFromPool( , which gets an object from the pool, and ReturnObjectToPool(), which returns object to the Pool. The object pool is implemented as two hashtables, one called locked and the other called unlocked. The locked hashtable contains all the objects that are currently in use and unlocked contains all the objects that are free and available for use. The ObjectPool also contains three MustOverride methods Create( , Validate(), and Expire(), that must be implemented by the derived classes.



In total, there are three critical sections in the ObjectPool class:

While getting an object to the pool, GetObjectFromPool( is used - A lock is needed while adding an object to the pool because the content of the locked and unlocked hashtables change and we do not want any race condition here.

While returning an object to the pool, ReturnObjectToPool( is used - Again, a lock is needed while returning an object to the pool because the content of the locked and unlocked hashtables will change and a new object will be available for use. Here also we cannot afford to have a race condition, because, we do not want multiple threads accessing the same hashtable at the same time.

While cleaning up the expired objects from the pool, CollectGarbage( - In this method, we go over the unlocked hashtable to find and remove expired objects from the pool. The content of the unlocked hashtable may change and we need the removal of expired objects to be atomic.

In the GetObjectFromPool( method, we iterate over the unlocked hashtable to get the first available object. The Validate( method is used to validate the specific object. The Validate( method may vary in specific implementations based on the type of the pooled object. For example, if the object is a database connection, the derived class of the object pool needs to implement the Validate( method to check whether the connection to the database is open or closed. If the validation of the pooled object succeeds, we remove the object from the unlocked hash table and put it in the locked hashtable. The locked hashtable contains the objects that are currently in use. If the validation fails, we kill the object with the Expire( method. The Expire( method also needs to be implemented by the derived class and is specific to the specific type of pooled object. For example, in the case of a database connection, the expired object will close the database connection. If a pooled object is not found, that is if the unlocked hashtable is empty, we create a new object using the Create( method and put the object in the locked hashtable.

The ReturnObjectToPool( method implementation is much simpler. We just have to remove the object from the locked hashtable and put it back in the unlocked hash table for recycling. In this whole recycling process, we have to take into consideration the memory usage of the application. Object pooling is directly proportional to memory usage. So, the more objects we pool, the more memory we will be using. To control memory usage, we should periodically garbage-collect the objects that are pooled. This can be achieved by assigning a timeout period to every pooled object. If the pooled object is not used within the timeout period, it will be garbage-collected. As a result, the memory usage of the object pool will vary depending on the load on the system. The CollectGarbage( method is used for handling the garbage collection of the pooled object. This method is called by the aTimer delegate that is initialized in the ObjectPool constructor. In our example, we set the garbage-collection interval to 90 seconds in the GARBAGE_COLLECT constant.

We haven't implemented any database connection-specific code so we can assume that the ObjectPool class can be used for the pooling any type of .NET Framework objects.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 766
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved