KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > util > Pool


1 /*
2  * Copyright (C) 2001 - 2004 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  */

19 package fr.dyade.aaa.util;
20
21 import org.objectweb.util.monolog.api.BasicLevel;
22 import org.objectweb.util.monolog.api.Logger;
23
24 public final class Pool {
25   int elementCount = 0;
26   Object JavaDoc[] elementData = null;
27
28   private Logger logmon = null;
29   private long cpt1, cpt2, alloc, free, min, max;
30
31   private String JavaDoc name;
32   private String JavaDoc logmsg;
33
34   public Pool(String JavaDoc name, int capacity) {
35     this.name = name;
36     this.logmsg = name + ".Pool: ";
37     elementData = new Object JavaDoc[capacity];
38     logmon = Debug.getLogger(getClass().getName() + '.' + getName());
39     logmon.log(BasicLevel.INFO, logmsg + capacity);
40   }
41
42   public String JavaDoc getName() {
43     return name;
44   }
45
46   public final synchronized void freeElement(Object JavaDoc obj) {
47     if (Debug.debug && logmon.isLoggable(BasicLevel.DEBUG)) {
48       logmon.log(BasicLevel.DEBUG,
49                  logmsg + "freeElement " + obj);
50     }
51     // If there is enough free element, let the gc get this element.
52
if (elementCount == elementData.length) {
53       free += 1;
54       return;
55     }
56     elementData[elementCount] = obj;
57     elementCount += 1;
58
59     if (elementCount > max) max = elementCount;
60   }
61
62   public final synchronized Object JavaDoc allocElement() throws Exception JavaDoc {
63     if (elementCount == 0) {
64       alloc += 1;
65
66       if (Debug.debug && logmon.isLoggable(BasicLevel.DEBUG))
67         logmon.log(BasicLevel.DEBUG, logmsg + "allocElement <new>");
68
69       throw new Exception JavaDoc();
70     }
71     elementCount -= 1;
72     Object JavaDoc obj = elementData[elementCount];
73     elementData[elementCount] = null; /* to let gc do its work */
74
75     if (elementCount < min) min = elementCount;
76     cpt1 += 1; cpt2 += elementCount;
77     if ((cpt1 & 0xFFFFFL) == 0L) {
78       if (logmon.isLoggable(BasicLevel.INFO)) {
79         logmon.log(BasicLevel.INFO,
80                    logmsg + (cpt2/cpt1) + '/' + elementCount +
81                    ", " + min + '/' + max + ", " + alloc + ", " + free);
82         alloc = 0; free = 0; min = elementData.length; max = 0;
83       }
84     }
85     
86     if (Debug.debug && logmon.isLoggable(BasicLevel.DEBUG)) {
87       logmon.log(BasicLevel.DEBUG,
88                  logmsg + "allocElement " + obj);
89     }
90
91     return obj;
92   }
93 }
94
Popular Tags