KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > pool > UnboundedPool


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 //NOTE: Tabs are used instead of spaces for indentation.
25
// Make sure that your editor does not replace tabs with spaces.
26
// Set the tab length using your favourite editor to your
27
// visual preference.
28

29 /*
30  * Filename: UnboundedPool.java
31  *
32  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
33  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
34  * All rights reserved.
35  *
36  * This software is the confidential and proprietary information
37  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
38  * You shall not disclose such Confidential Information and shall
39  * use it only in accordance with the terms of the license
40  * agreement you entered into with iPlanet/Sun Microsystems.
41  */

42  
43 /**
44  * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/pool/UnboundedPool.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:29 $
47  */

48  
49 package com.sun.enterprise.util.pool;
50
51 /**
52  * An UnboundedPool can be used to create a pool of unlimited size. All getObject(....) methods
53  * are guaranteed to return an object irrespective of the wait flag and wait time. Note that
54  * if the objects held in the pool consume a siginificant memory, then maintaining a large
55  * UnboundedPool may cause java.lang.OutOfMemory error (which probably would not have occured
56  * if there was no pooling!!). If memory is an issue then use SoftUnboundedPool.
57  *
58  * <p> The initial size of the pool and the load factor of the pool determine how the
59  * pool size adjusts dynamically. For example, if the initial pool size is 100 and if the load factor
60  * is 90, then as soon as 90% of the pool objects are used (given out), then the pool size grows
61  * by 10.
62  */

63 public class UnboundedPool
64     extends ArrayListPool
65 {
66     private int initialSize;
67     
68     /**
69      * Create an Unbounded pool.
70      * @param The ObjectFactory to create objects
71      * @param The initial number of objects to be held in the pool
72      * @param The load factor. This value indicates when and how much the pool
73      * should expand / shrink. Both initialSize and loadFactor are used to
74      * compute the new size during expansion / shrinking.
75      */

76     public UnboundedPool(ObjectFactory factory, int initialSize) {
77         super(factory, initialSize);
78         this.initialSize = initialSize;
79         super.preload(initialSize);
80     }
81     
82     /**
83      * Since this method would be called only if the pool is empty,
84      * and since this an unbounded pool, CREATE IT!!
85      */

86     protected boolean canCreate() {
87         return true;
88     }
89     
90     /**
91      * Notification when an object is put back into the pool (checkin).
92      * @param The object to be returned back to the pool.
93      * @return Any non null value can be returned to signal that the object
94      * was indeed added to the pool. This class always adds the object to the
95      * pool (at the end of the list), it returns non-null value.
96      * Subclasses can override this behaviour.
97      */

98     protected Object JavaDoc checkin(Object JavaDoc object) {
99         if (waitCount == 0) {
100             int diff = arrayList.size() - initialSize;
101             if (diff > initialSize) {
102                 super.destroyPoolObjects(diff);
103             }
104         }
105         
106         arrayList.add(object);
107         return this;
108     }
109     
110 }
Popular Tags