1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 19 public class PrototyperThread extends Thread { 20 21 private static final ThreadGroup PROTOTYPER_THREAD_GROUP = new ThreadGroup ("PROTOTYPER_THREAD_GROUP"); 22 23 private static final Log LOG = LogFactory.getLog(PrototyperThread.class); 24 25 private boolean stop; 26 27 public PrototyperThread(String name) { 28 super(PROTOTYPER_THREAD_GROUP, name); 29 setDaemon(true); 30 } 31 32 public void run() { 33 34 while (!stop) { 35 int sweptCount = 0; 36 while (PrototyperController.isKeepSweeping() && !stop) { 37 PrototyperController.sweepStarted(); 38 ConnectionPool[] cps = ConnectionPoolManager.getInstance().getConnectionPools(); 39 for (int i = 0; i < cps.length && !stop; i++) { 40 Prototyper p = cps[i].getPrototyper(); 41 try { 42 cps[i].acquirePrimaryReadLock(); 43 if (cps[i].isConnectionPoolUp() && p.isSweepNeeded()) { 44 p.sweep(); 45 sweptCount++; 46 } 47 } catch (InterruptedException e) { 48 LOG.error("Couldn't acquire primary read lock", e); 49 } finally { 50 cps[i].releasePrimaryReadLock(); 51 } 52 } 53 } 54 58 doWait(); 59 } 60 } 61 62 protected void cancel() { 63 stop = true; 64 doNotify(); 65 } 66 67 private synchronized void doWait() { 68 try { 69 wait(); 70 } catch (InterruptedException e) { 71 LOG.debug("Expected interruption of sleep"); 72 } 73 } 74 75 protected synchronized void doNotify() { 76 notifyAll(); 77 } 78 79 } 80 81 82 | Popular Tags |