KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > core > RequestMonitor


1 /**
2  * Copyright (C) 2004 andres
3  * Created: 08.11.2004 (10:54:00)
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */

19 package freecs.core;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import freecs.Server;
25
26
27 /**
28  * @author andres
29  *
30  * TODO To change the template for this generated type comment go to
31  * Window - Preferences - Java - Code Generation - Code and Comments
32  */

33 public class RequestMonitor extends Thread JavaDoc {
34     public static RequestMonitor instance = new RequestMonitor();
35     private HashMap JavaDoc monitors = new HashMap JavaDoc();
36
37     private RequestMonitor () {
38     }
39     
40     public synchronized void addMonitor (Thread JavaDoc t, long timeout) {
41         if (!this.isAlive())
42             this.start();
43         if (t == null)
44             return;
45         monitors.put(t, new Long JavaDoc(timeout));
46     }
47
48     public synchronized void removeMonitor (Thread JavaDoc t) {
49         monitors.remove(t);
50     }
51     
52     private void interruptMonitored(Thread JavaDoc t) {
53         t.interrupt();
54         /* if (t instanceof RequestReader) {
55             ((RequestReader) t).cancelRequest();
56         }*/

57         Server.log ("RequestMonitor", "interrupted thread " + t.toString(), Server.MSG_STATE, Server.LVL_MAJOR);
58     }
59     
60     public void run () {
61         long lastMessage = 0;
62         while (Server.srv.isRunning()) {
63             if (Server.DEBUG || lastMessage + 5000 > System.currentTimeMillis()) {
64                 Server.log (this, "loopstart", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
65                 lastMessage = System.currentTimeMillis();
66             }
67             long sleepTime = 1000;
68             long now = System.currentTimeMillis();
69             synchronized (this) {
70                 for (Iterator JavaDoc i = monitors.keySet().iterator(); i.hasNext(); ) {
71                     Thread JavaDoc t = (Thread JavaDoc) i.next();
72                     long timeout = ((Long JavaDoc) monitors.get(t)).longValue();
73                     if (timeout <= now) {
74                         interruptMonitored(t);
75                         i.remove();
76                         continue;
77                     } else {
78                         long thisTimeout = now - timeout;
79                         if (thisTimeout < sleepTime)
80                             sleepTime = thisTimeout;
81                     }
82                 }
83             }
84             if (sleepTime < 33)
85                 sleepTime = 33;
86             try {
87                 Thread.sleep(sleepTime);
88             } catch (InterruptedException JavaDoc ie) {
89                 // doesn't happen
90
}
91         }
92     }
93     
94     public String JavaDoc toString () {
95         return "[RequestMonitor]";
96     }
97 }
Popular Tags