Creating Multiple Pools of Objects of Variable Size in Java
Object pool pattern is a software creational design pattern that is used in situations where the cost of initializing a class instance is very high. Basically, an Object pool is a container that contains some amount of objects. So, when an object is taken from the pool, it is not available in the pool until it is put back.
Objects in the pool have a lifecycle:
Object pooling can provide a large speed improvement; it works best when the cost of initializing a class instance is high, the pace of instantiation is high, and the number of instantiations in use at any given moment is low.
The Object Pool design is useful when objects are expensive to construct and only need to be used for a short period of time. In this design pattern, we keep track of which instantiated objects are in use and which are accessible.
How to create Multiple pools of Objects?
We all know how to create a single pool of objects. But there may be a requirement to create multiple pools of objects of variable size. This means instead of creating a single pool of objects o fixed size, we can have multiple pools & each pool has a different number of total objects.
What is the real-time requirement for it?
Memory allocation as needed for error handling or in emergent scenarios in programming can be accomplished by generating multiple variable-size memory pools.
There are a few objects for which the production of a new object appears to be slightly more expensive since they are not considered lightweight. Database connection objects, parser objects, thread generation, and so forth are examples. We may have to construct numerous such pools of objects in any application. Because the generation of such objects is expensive, it will have a negative impact on the performance of any program. We can use the same object again & again.
We are here creating a class “Thread” whose Objects are there in pools & another class “ThreadPool” that is used to create multiple pools of “Thread” Objects.
In our class Thread class(class 1)
We have data members:
- “uniqueId” Stores unique id generated for each thread object.
- “name” Keeps the name for the thread object.
In our class ThreadPool class(class 2)
We have data members:
- isFirst = true to keep true initially & whenever 1st Thread object in pool is created, after 1st object creation, it is set to false.
- myThreads of type Thread which is an array reference that will refer to the Thread Class object(Thread Pool Array)
- isUsed of type boolean which is an array to Keep track that which Thread’s objects are in use currently
- noOfThreadPools to keep count of no of Thread Pools & is a class level Property
- ThreadreferenceCount to keep count of no of thread references in a particular pool
- poolNum to store unique number given to every Pool Created
*****************POOL1 Of Threads Created***************** Pool 1 initialized 1 is the thread number of the pool 1 2 is the thread number of the pool 1 t3 do not refer any object Pool 1 not initialized as it was already initialized *****************POOL2 Of Threads Created***************** 1 is the thread number of the pool 2
- Here, we create two pools of Thread Objects by:
ThreadPool p1 = new ThreadPool() & ThreadPool p2 = new ThreadPool()
- Then we called initialize() method on p1 & p2 giving size (total “Thread” Objects in the pool ) – 2 & 3 respectively.
- Then 2 references of Thread class ( t1 & t2) are created who are referencing Objects (of Thread type) in Pool p1.
- A 3rd reference(t3) of Thread class is created, and we are trying to get Object (by calling getReference() on p1)from pool p1 so that t3 can refer it. But t3 do not refer to any object as Pool p1 size is 2, only 2 references from the outside world can refer to the 2 objects in the pool.
- Then we release both the references (t1 & t2) by calling the method releaseReference(). Now the objects in the pool are available & the outside world references can refer them by calling the method getReference() on p1.
- Similarly, we created another reference (tp1) but it refers to the Thread object contained in the pool p2 as we called the method getReference() on p2.
- Then we release the references (tp1) by calling the method releaseReference().
- At the end we call method deInitialize() on p1 & p2 bring back p1 & p2 pool to the starting condition that was before calling initialize().
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.