- /**
- * This is the essential function for Pool. Get an Object "T" inside a
- * "Pooled<T>" object. If there is a spare Object, then use it. If not, then
- * create and pass back.
- *
- * This one uses a Null LogTarget
- *
- * IMPORTANT: When the use of this object is done (and the object is still
- * in a valid state), then "done()" should be called immediately to allow
- * the object to be reused. That is the point of the Pool...
- *
- * If the Object is in an invalid state, then "toss()" should be used so the
- * Pool doesn't pass on invalid objects to others.
- *
- * @param lt
- * @return
- * @throws CadiException
- */
- public Pooled<T> get() throws CadiException {
- Pooled<T> pt;
- synchronized (list) {
- if (list.isEmpty()) {
- pt = null;
- } else {
- pt = list.removeLast();
- --count;
- creator.reuse(pt.content);
- }
- }
- if (pt == null) {
- if (spares < max_range)
- ++spares;
- pt = new Pooled<T>(creator.create(), this);
- } else {
- if (spares > 1)
- --spares;
- }
- return pt;
- }
+ /**
+ * Destroy and remove all remaining objects. This is valuable for closing
+ * down all Allocated objects cleanly for exiting. It is also a good method
+ * for removing objects when, for instance, all Objects are invalid because
+ * of broken connections, etc.
+ *
+ * Use in conjunction with setMaxRange to no longer store objects, i.e.
+ *
+ * pool.setMaxRange(0).drain();
+ */
+ public synchronized void drain() {
+ while(list.size()>0) {
+ Pooled<T> pt = list.remove();
+ --used;
+ String name = pt.content.toString();
+ creator.destroy(pt.content);
+ logger.log(Log.Type.debug,"Pool destroyed", name);
+ }
+ count = 0;
+ }
+
+ /**
+ * This is the essential function for Pool. Get an Object "T" inside a
+ * "Pooled<T>" object. If there is a spare Object, then use it. If not, then
+ * create and pass back.
+ *
+ * This one uses a Null LogTarget
+ *
+ * IMPORTANT: When the use of this object is done (and the object is still
+ * in a valid state), then "done()" should be called immediately to allow
+ * the object to be reused. That is the point of the Pool...
+ *
+ * If the Object is in an invalid state, then "toss()" should be used so the
+ * Pool doesn't pass on invalid objects to others.
+ *
+ * @param lt
+ * @return
+ * @throws CadiException
+ */
+ public Pooled<T> get() throws CadiException {
+ Pooled<T> pt;
+ synchronized (list) {
+ pt = list.pollLast();
+ }
+ if (pt == null) {
+ pt = new Pooled<T>(creator.create(), this);
+ ++used;
+ } else {
+ --count;
+ creator.reuse(pt.content);
+ }
+ return pt;
+ }