001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: /**
012: * Responsible for running {@link Prototyper#sweep sweep}. There
013: * could be just one of the objects, or more.
014: * @version $Revision: 1.6 $, $Date: 2006/01/18 14:40:01 $
015: * @author bill
016: * @author $Author: billhorsman $ (current maintainer)
017: * @since Proxool 0.8
018: */
019: public class PrototyperThread extends Thread {
020:
021: private static final ThreadGroup PROTOTYPER_THREAD_GROUP = new ThreadGroup(
022: "PROTOTYPER_THREAD_GROUP");
023:
024: private static final Log LOG = LogFactory
025: .getLog(PrototyperThread.class);
026:
027: private boolean stop;
028:
029: public PrototyperThread(String name) {
030: super (PROTOTYPER_THREAD_GROUP, name);
031: setDaemon(true);
032: }
033:
034: public void run() {
035:
036: while (!stop) {
037: int sweptCount = 0;
038: while (PrototyperController.isKeepSweeping() && !stop) {
039: PrototyperController.sweepStarted();
040: ConnectionPool[] cps = ConnectionPoolManager
041: .getInstance().getConnectionPools();
042: for (int i = 0; i < cps.length && !stop; i++) {
043: Prototyper p = cps[i].getPrototyper();
044: try {
045: cps[i].acquirePrimaryReadLock();
046: if (cps[i].isConnectionPoolUp()
047: && p.isSweepNeeded()) {
048: p.sweep();
049: sweptCount++;
050: }
051: } catch (InterruptedException e) {
052: LOG.error("Couldn't acquire primary read lock",
053: e);
054: } finally {
055: cps[i].releasePrimaryReadLock();
056: }
057: }
058: }
059: // if (LOG.isDebugEnabled()) {
060: // LOG.debug("Swept " + sweptCount + " pools");
061: // }
062:
063: doWait();
064: }
065: }
066:
067: protected void cancel() {
068: stop = true;
069: doNotify();
070: }
071:
072: private synchronized void doWait() {
073: try {
074: wait();
075: } catch (InterruptedException e) {
076: LOG.debug("Expected interruption of sleep");
077: }
078: }
079:
080: protected synchronized void doNotify() {
081: notifyAll();
082: }
083:
084: }
085:
086: /*
087: Revision history:
088: $Log: PrototyperThread.java,v $
089: Revision 1.6 2006/01/18 14:40:01 billhorsman
090: Unbundled Jakarta's Commons Logging.
091:
092: Revision 1.5 2004/03/26 15:58:56 billhorsman
093: Fixes to ensure that house keeper and prototyper threads finish after shutdown.
094:
095: Revision 1.4 2003/04/10 08:23:55 billhorsman
096: removed very frequent debug
097:
098: Revision 1.3 2003/03/10 23:43:12 billhorsman
099: reapplied checkstyle that i'd inadvertently let
100: IntelliJ change...
101:
102: Revision 1.2 2003/03/10 15:26:48 billhorsman
103: refactoringn of concurrency stuff (and some import
104: optimisation)
105:
106: Revision 1.1 2003/03/05 18:42:33 billhorsman
107: big refactor of prototyping and house keeping to
108: drastically reduce the number of threads when using
109: many pools
110:
111: */
|