Mercurial Hosting > luan
annotate src/org/eclipse/jetty/util/thread/Timeout.java @ 941:c948f674a2d5
simplify Timeout
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 11 Oct 2016 00:34:19 -0600 |
parents | 8e9db0bbf4f9 |
children | c157a786ed0b |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.util.thread; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
21 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
22 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 /* ------------------------------------------------------------ */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 /** Timeout queue. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 * This class implements a timeout queue for timers that are at least as likely to be cancelled as they are to expire. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 * Unlike the util timeout class, the duration of the timeouts is shared by all scheduled tasks and if the duration |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 * is changed, this affects all scheduled tasks. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 * <p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 * The nested class Task should be extended by users of this class to obtain call back notification of |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 * expires. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 public class Timeout |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 { |
941 | 36 private static final Logger LOG = LoggerFactory.getLogger(Timeout.class); |
37 private Object _lock; | |
38 private long _duration; | |
39 private volatile long _now=System.currentTimeMillis(); | |
40 private Task _head=new Task(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 |
941 | 42 /* ------------------------------------------------------------ */ |
43 public Timeout(Object lock) | |
44 { | |
45 _lock=lock; | |
46 _head._timeout=this; | |
47 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 |
941 | 49 /* ------------------------------------------------------------ */ |
50 /** | |
51 * @return Returns the duration. | |
52 */ | |
53 public long getDuration() | |
54 { | |
55 return _duration; | |
56 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 |
941 | 58 /* ------------------------------------------------------------ */ |
59 /** | |
60 * @param duration The duration to set. | |
61 */ | |
62 public void setDuration(long duration) | |
63 { | |
64 _duration = duration; | |
65 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
66 |
941 | 67 /* ------------------------------------------------------------ */ |
68 public long setNow() | |
69 { | |
70 return _now=System.currentTimeMillis(); | |
71 } | |
72 | |
73 /* ------------------------------------------------------------ */ | |
74 public long getNow() | |
75 { | |
76 return _now; | |
77 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
78 |
941 | 79 /* ------------------------------------------------------------ */ |
80 public void setNow(long now) | |
81 { | |
82 _now=now; | |
83 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
84 |
941 | 85 /* ------------------------------------------------------------ */ |
86 /** Get an expired tasks. | |
87 * This is called instead of {@link #tick()} to obtain the next | |
88 * expired Task, but without calling it's {@link Task#expire()} or | |
89 * {@link Task#expired()} methods. | |
90 * | |
91 * @return the next expired task or null. | |
92 */ | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
93 |
941 | 94 public Task expired() |
95 { | |
96 synchronized (_lock) | |
97 { | |
98 long _expiry = _now-_duration; | |
99 if (_head._next!=_head) | |
100 { | |
101 Task task = _head._next; | |
102 if (task._timestamp>_expiry) | |
103 return null; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
104 |
941 | 105 task.unlink(); |
106 task._expired=true; | |
107 return task; | |
108 } | |
109 return null; | |
110 } | |
111 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
112 |
941 | 113 public void cancelAll() |
114 { | |
115 synchronized (_lock) | |
116 { | |
117 _head._next=_head._prev=_head; | |
118 } | |
119 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
120 |
941 | 121 public long getTimeToNext() |
122 { | |
123 synchronized (_lock) | |
124 { | |
125 if (_head._next==_head) | |
126 return -1; | |
127 long to_next = _duration+_head._next._timestamp-_now; | |
128 return to_next<0?0:to_next; | |
129 } | |
130 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
131 |
941 | 132 @Override |
133 public String toString() | |
134 { | |
135 StringBuffer buf = new StringBuffer(); | |
136 buf.append(super.toString()); | |
137 | |
138 Task task = _head._next; | |
139 while (task!=_head) | |
140 { | |
141 buf.append("-->"); | |
142 buf.append(task); | |
143 task=task._next; | |
144 } | |
145 | |
146 return buf.toString(); | |
147 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
148 |
941 | 149 /* ------------------------------------------------------------ */ |
150 /* ------------------------------------------------------------ */ | |
151 /* ------------------------------------------------------------ */ | |
152 /* ------------------------------------------------------------ */ | |
153 /** Task. | |
154 * The base class for scheduled timeouts. This class should be | |
155 * extended to implement the expire() method, which is called if the | |
156 * timeout expires. | |
157 * | |
158 * | |
159 * | |
160 */ | |
161 public static class Task | |
162 { | |
163 Task _next; | |
164 Task _prev; | |
165 Timeout _timeout; | |
166 long _timestamp=0; | |
167 boolean _expired=false; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
168 |
941 | 169 protected Task() |
170 { | |
171 _next=_prev=this; | |
172 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
173 |
941 | 174 private void unlink() |
175 { | |
176 _next._prev=_prev; | |
177 _prev._next=_next; | |
178 _next=_prev=this; | |
179 _expired=false; | |
180 } | |
181 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
182 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
183 } |