KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > PrototyperThread


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 /**
12  * Responsible for running {@link Prototyper#sweep sweep}. There
13  * could be just one of the objects, or more.
14  * @version $Revision: 1.6 $, $Date: 2006/01/18 14:40:01 $
15  * @author bill
16  * @author $Author: billhorsman $ (current maintainer)
17  * @since Proxool 0.8
18  */

19 public class PrototyperThread extends Thread JavaDoc {
20
21     private static final ThreadGroup JavaDoc PROTOTYPER_THREAD_GROUP = new ThreadGroup JavaDoc("PROTOTYPER_THREAD_GROUP");
22
23     private static final Log LOG = LogFactory.getLog(PrototyperThread.class);
24
25     private boolean stop;
26
27     public PrototyperThread(String JavaDoc 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 JavaDoc e) {
48                         LOG.error("Couldn't acquire primary read lock", e);
49                     } finally {
50                         cps[i].releasePrimaryReadLock();
51                     }
52                 }
53             }
54 // if (LOG.isDebugEnabled()) {
55
// LOG.debug("Swept " + sweptCount + " pools");
56
// }
57

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 JavaDoc e) {
71             LOG.debug("Expected interruption of sleep");
72         }
73     }
74
75     protected synchronized void doNotify() {
76         notifyAll();
77     }
78
79 }
80
81
82 /*
83  Revision history:
84  $Log: PrototyperThread.java,v $
85  Revision 1.6 2006/01/18 14:40:01 billhorsman
86  Unbundled Jakarta's Commons Logging.
87
88  Revision 1.5 2004/03/26 15:58:56 billhorsman
89  Fixes to ensure that house keeper and prototyper threads finish after shutdown.
90
91  Revision 1.4 2003/04/10 08:23:55 billhorsman
92  removed very frequent debug
93
94  Revision 1.3 2003/03/10 23:43:12 billhorsman
95  reapplied checkstyle that i'd inadvertently let
96  IntelliJ change...
97
98  Revision 1.2 2003/03/10 15:26:48 billhorsman
99  refactoringn of concurrency stuff (and some import
100  optimisation)
101
102  Revision 1.1 2003/03/05 18:42:33 billhorsman
103  big refactor of prototyping and house keeping to
104  drastically reduce the number of threads when using
105  many pools
106
107  */
Popular Tags