KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > PoolGCThread


1 /*
2  * Licensed under the X license (see http://www.x.org/terms.htm)
3  */

4 package org.ofbiz.minerva.pool;
5
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8
9 import org.apache.log4j.Logger;
10
11 /**
12  * Runs garbage collection on all available pools. Only one GC thread is
13  * created, no matter how many pools there are - it just tries to calculate
14  * the next time it should run based on the figues for all the pools. It will
15  * run on any pools which are "pretty close" to their requested time.
16  *
17  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
18  * @version $Rev: 5462 $
19  */

20 class PoolGCThread extends Thread JavaDoc {
21
22     private HashSet JavaDoc pools = new HashSet JavaDoc();
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             // Don't do anything while there's nothing to do
34
waitForPools();
35             if (trace)
36                 log.debug("gc thread waited for pools");
37
38             // Figure out how long to sleep
39
long delay = getDelay();
40             if (trace)
41                 log.debug("gc thread delay: " + delay);
42
43             // Sleep
44
if (delay > 0l) {
45                 try {
46                     sleep(delay);
47                 } catch (InterruptedException JavaDoc ignored) {
48                 }
49             }
50
51             // Run garbage collection on eligible pools
52
runGC();
53         }
54     }
55
56     private synchronized void waitForPools() {
57         while (pools.size() == 0) {
58             try {
59                 wait();
60             } catch (InterruptedException JavaDoc ignored) {
61                 // Warning level seems appropriate; shouldn't really happen
62
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 JavaDoc 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 JavaDoc 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 /*
117 vim:tabstop=3:et:shiftwidth=3
118 */

119
Popular Tags