1 4 package org.ofbiz.minerva.pool; 5 6 import java.util.HashSet ; 7 import java.util.Iterator ; 8 9 import org.apache.log4j.Logger; 10 11 20 class PoolGCThread extends Thread { 21 22 private HashSet pools = new HashSet (); 23 private static Logger log = Logger.getLogger(ObjectPool.class); 24 25 PoolGCThread() { 26 super("Minerva ObjectPool GC Thread"); 27 setDaemon(true); 28 } 29 30 public void run() { 31 boolean trace = true; 32 while (true) { 33 waitForPools(); 35 if (trace) 36 log.debug("gc thread waited for pools"); 37 38 long delay = getDelay(); 40 if (trace) 41 log.debug("gc thread delay: " + delay); 42 43 if (delay > 0l) { 45 try { 46 sleep(delay); 47 } catch (InterruptedException ignored) { 48 } 49 } 50 51 runGC(); 53 } 54 } 55 56 private synchronized void waitForPools() { 57 while (pools.size() == 0) { 58 try { 59 wait(); 60 } catch (InterruptedException ignored) { 61 log.warn("waitForPools interrupted"); 63 } 64 } 65 } 66 67 private synchronized long getDelay() { 68 long next = Long.MAX_VALUE; 69 long now = System.currentTimeMillis(); 70 long current; 71 72 for (Iterator it = pools.iterator(); it.hasNext();) { 73 ObjectPool pool = (ObjectPool) it.next(); 74 current = pool.getNextGCMillis(now); 75 if (current < next) 76 next = current; 77 } 78 return next >= 0l ? next : 0l; 79 } 80 81 private synchronized void runGC() { 82 boolean trace = true; 83 84 if (trace) 85 log.debug("GC thread running GC"); 86 87 for (Iterator it = pools.iterator(); it.hasNext();) { 88 ObjectPool pool = (ObjectPool) it.next(); 89 90 if (trace) 91 log.debug("GC Thread pool: " + pool.getName() + ", isTimeToGC(): " + pool.isTimeToGC()); 92 93 if (pool.isTimeToGC()) 94 pool.runGCandShrink(); 95 } 96 } 97 98 synchronized void addPool(ObjectPool pool) { 99 if (log.isDebugEnabled()) 100 log.debug("Adding pool: " + pool.getName() + ", GC enabled: " + pool.isGCEnabled()); 101 102 if (pool.isGCEnabled()) 103 pools.add(pool); 104 105 notify(); 106 } 107 108 synchronized void removePool(ObjectPool pool) { 109 if (log.isDebugEnabled()) 110 log.debug("Removing pool: " + pool.getName()); 111 112 pools.remove(pool); 113 } 114 } 115 116 119 | Popular Tags |