KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > util > TimeoutService


1
2 package ch.ethz.ssh2.util;
3
4 import java.io.PrintWriter JavaDoc;
5 import java.io.StringWriter JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.LinkedList JavaDoc;
8
9 import ch.ethz.ssh2.log.Logger;
10
11 /**
12  * TimeoutService (beta). Here you can register a timeout.
13  * <p>
14  * Implemented having large scale programs in mind: if you open many concurrent SSH connections
15  * that rely on timeouts, then there will be only one timeout thread. Once all timeouts
16  * have expired/are cancelled, the thread will (sooner or later) exit.
17  * Only after new timeouts arrive a new thread (singleton) will be instantiated.
18  *
19  * @author Christian Plattner, plattner@inf.ethz.ch
20  * @version $Id: TimeoutService.java,v 1.2 2006/07/30 21:59:29 cplattne Exp $
21  */

22 public class TimeoutService
23 {
24     private static final Logger log = Logger.getLogger(TimeoutService.class);
25
26     public static class TimeoutToken implements Comparable JavaDoc
27     {
28         private long runTime;
29         private Runnable JavaDoc handler;
30
31         private TimeoutToken(long runTime, Runnable JavaDoc handler)
32         {
33             this.runTime = runTime;
34             this.handler = handler;
35         }
36
37         public int compareTo(Object JavaDoc o)
38         {
39             TimeoutToken t = (TimeoutToken) o;
40             if (runTime > t.runTime)
41                 return 1;
42             if (runTime == t.runTime)
43                 return 0;
44             return -1;
45         }
46     }
47
48     private static class TimeoutThread extends Thread JavaDoc
49     {
50         public void run()
51         {
52             synchronized (todolist)
53             {
54                 while (true)
55                 {
56                     if (todolist.size() == 0)
57                     {
58                         timeoutThread = null;
59                         return;
60                     }
61
62                     long now = System.currentTimeMillis();
63
64                     TimeoutToken tt = (TimeoutToken) todolist.getFirst();
65
66                     if (tt.runTime > now)
67                     {
68                         /* Not ready yet, sleep a little bit */
69
70                         try
71                         {
72                             todolist.wait(tt.runTime - now);
73                         }
74                         catch (InterruptedException JavaDoc e)
75                         {
76                         }
77
78                         /* We cannot simply go on, since it could be that the token
79                          * was removed (cancelled) or another one has been inserted in
80                          * the meantime.
81                          */

82
83                         continue;
84                     }
85
86                     todolist.removeFirst();
87
88                     try
89                     {
90                         tt.handler.run();
91                     }
92                     catch (Exception JavaDoc e)
93                     {
94                         StringWriter JavaDoc sw = new StringWriter JavaDoc();
95                         e.printStackTrace(new PrintWriter JavaDoc(sw));
96                         log.log(20, "Exeception in Timeout handler:" + e.getMessage() + "(" + sw.toString() + ")");
97                     }
98                 }
99             }
100         }
101     }
102
103     /* The list object is also used for locking purposes */
104     private static final LinkedList JavaDoc todolist = new LinkedList JavaDoc();
105
106     private static Thread JavaDoc timeoutThread = null;
107
108     /**
109      * It is assumed that the passed handler will not execute for a long time.
110      *
111      * @param runTime
112      * @param handler
113      * @return a TimeoutToken that can be used to cancel the timeout.
114      */

115     public static final TimeoutToken addTimeoutHandler(long runTime, Runnable JavaDoc handler)
116     {
117         TimeoutToken token = new TimeoutToken(runTime, handler);
118
119         synchronized (todolist)
120         {
121             todolist.add(token);
122             Collections.sort(todolist);
123
124             if (timeoutThread != null)
125                 timeoutThread.interrupt();
126             else
127             {
128                 timeoutThread = new TimeoutThread();
129                 timeoutThread.setDaemon(true);
130                 timeoutThread.start();
131             }
132         }
133
134         return token;
135     }
136
137     public static final void cancelTimeoutHandler(TimeoutToken token)
138     {
139         synchronized (todolist)
140         {
141             todolist.remove(token);
142
143             if (timeoutThread != null)
144                 timeoutThread.interrupt();
145         }
146     }
147
148 }
149
Popular Tags