1 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 [] elementData = null; 27 28 private Logger logmon = null; 29 private long cpt1, cpt2, alloc, free, min, max; 30 31 private String name; 32 private String logmsg; 33 34 public Pool(String name, int capacity) { 35 this.name = name; 36 this.logmsg = name + ".Pool: "; 37 elementData = new Object [capacity]; 38 logmon = Debug.getLogger(getClass().getName() + '.' + getName()); 39 logmon.log(BasicLevel.INFO, logmsg + capacity); 40 } 41 42 public String getName() { 43 return name; 44 } 45 46 public final synchronized void freeElement(Object obj) { 47 if (Debug.debug && logmon.isLoggable(BasicLevel.DEBUG)) { 48 logmon.log(BasicLevel.DEBUG, 49 logmsg + "freeElement " + obj); 50 } 51 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 allocElement() throws Exception { 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 (); 70 } 71 elementCount -= 1; 72 Object obj = elementData[elementCount]; 73 elementData[elementCount] = null; 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 |