KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > util > SimplePool


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.jasper.util;
19
20 /**
21  * Simple object pool. Based on ThreadPool and few other classes
22  *
23  * The pool will ignore overflow and return null if empty.
24  *
25  * @author Gal Shachor
26  * @author Costin
27  */

28 public final class SimplePool {
29
30     private static final int DEFAULT_SIZE=16;
31
32     /*
33      * Where the threads are held.
34      */

35     private Object JavaDoc pool[];
36
37     private int max;
38     private int current=-1;
39
40     private Object JavaDoc lock;
41     
42     public SimplePool() {
43     this.max=DEFAULT_SIZE;
44     this.pool=new Object JavaDoc[max];
45     this.lock=new Object JavaDoc();
46     }
47     
48     public SimplePool(int max) {
49     this.max=max;
50     this.pool=new Object JavaDoc[max];
51     this.lock=new Object JavaDoc();
52     }
53
54     /**
55      * Adds the given object to the pool, and does nothing if the pool is full
56      */

57     public void put(Object JavaDoc o) {
58     synchronized( lock ) {
59         if( current < (max-1) ) {
60         current += 1;
61         pool[current] = o;
62             }
63     }
64     }
65
66     /**
67      * Get an object from the pool, null if the pool is empty.
68      */

69     public Object JavaDoc get() {
70     Object JavaDoc item = null;
71     synchronized( lock ) {
72         if( current >= 0 ) {
73         item = pool[current];
74         current -= 1;
75         }
76     }
77     return item;
78     }
79
80     /**
81      * Return the size of the pool
82      */

83     public int getMax() {
84     return max;
85     }
86 }
87
Popular Tags