GeeCON Prague 2014*
The moose likes Programming Diversions and the fly likes How to implement a Object pool in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "How to implement a Object pool in java" Watch "How to implement a Object pool in java" New topic
Author

How to implement a Object pool in java

Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108

Hi ranchers,
If I want to implement a Object pool in java using the built in data structures and the pros and cons, how do I go ahead and implement one.

Here is my approach. But I am sure there are many ways to implement and which is more efficient.

1. Implement using a queue. Its better to use arrays in the background and expose it via an Interface.
2. To implement using the LInkedList as it implements the Queue interface internally but exposing through an Interface which has only get and return method which will delegate to the internal linkedlist to return the first in element and to put it back to the Queue.

3. Implement using a PriorityQueue.

Any better way to do this in Java?

Regards,
Pradeep


Thanks and Regards, Pradeep Kumar
SCJP 1.6, SCWCD 5.0
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Well, a LinkedList already is a Queue. So that makes the first two the same thing. As for a PriorityQueue, I would only use that if there was some reason to put the objects in the pool into a certain sequence. Usually there isn't any such reason in an object pool, so I normally wouldn't consider that.
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108

Are there any other data structures which is an optimum for implementation apart from Queue?
 
GeeCON Prague 2014
 
subject: How to implement a Object pool in java