KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > transaction > Timer


1 package org.jacorb.transaction;
2
3
4
5 /*
6
7  * JacORB transaction service - a free TS for JacORB
8
9  *
10
11  * Copyright (C) 1999-2004 LogicLand group Alex Sinishin.
12
13  *
14
15  * This library is free software; you can redistribute it and/or
16
17  * modify it under the terms of the GNU Library General Public
18
19  * License as published by the Free Software Foundation; either
20
21  * version 2 of the License, or (at your option) any later version.
22
23  *
24
25  * This library is distributed in the hope that it will be useful,
26
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30
31  * Library General Public License for more details.
32
33  *
34
35  * You should have received a copy of the GNU Library General Public
36
37  * License along with this library; if not, write to the Free
38
39  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
40
41  *
42
43  * Changes made by Vladimir Mencl <vladimir.mencl@mff.cuni.cz> (2002/05/01)
44
45  *
46
47  * bug-fix: check for a null reference in kill_channel.
48
49  *
50
51  */

52
53
54
55
56
57 import java.util.*;
58
59
60 public class Timer extends Thread JavaDoc{
61
62
63
64     private boolean stoped;
65
66
67
68     class Channel{
69
70         Sleeper slp;
71
72         int time;
73
74
75
76         Channel(Sleeper _slp, int _time){
77
78             slp = _slp;
79
80             time = _time;
81
82         }
83
84     }
85
86
87
88     private Channel[] channels;
89
90     private int top;
91
92
93
94     Timer(int max_of_chan){
95
96         stoped = true;
97
98         channels = new Channel [max_of_chan];
99
100         for (int i = 0;i < channels.length;i++){
101
102             channels[i] = null;
103
104         }
105
106         top = 0;
107
108         start();
109
110     }
111
112
113
114     private int find_free(){
115
116         for (int i = 0;i < channels.length;i++){
117
118             if (channels[i] == null){
119
120                 return i;
121
122             }
123
124         }
125
126         throw new org.omg.CORBA.INTERNAL JavaDoc();
127
128     }
129
130
131
132     void pause(){
133
134         synchronized(channels){
135
136             if (!stoped){
137
138                 stoped = true;
139
140             }
141
142         }
143
144     }
145
146
147
148     void go(){
149
150         synchronized(channels){
151
152             if (stoped){
153
154                 stoped = false;
155
156                 resume();
157
158             }
159
160         }
161
162     }
163
164
165
166     void add_channel(Sleeper slp, int time){
167
168         if (time <= 0){
169
170             throw new org.omg.CORBA.INTERNAL JavaDoc();
171
172         }
173
174         Channel ch = new Channel(slp, time);
175
176
177
178         synchronized(channels){
179
180             int ix = find_free();
181
182             if (ix > top){
183
184                 throw new org.omg.CORBA.INTERNAL JavaDoc();
185
186             }
187
188             if (ix == top){
189
190                 top++;
191
192             }
193
194             channels[ix] = ch;
195
196         }
197
198
199
200         go();
201
202     }
203
204
205
206     private void destroy_channel(int ix){
207
208         synchronized(channels){
209
210             channels[ix] = null;
211
212             if ((top - 1) == ix){
213
214                 top--;
215
216             }
217
218             if (top == 0){
219
220                 stoped = true;
221
222             }
223
224         }
225
226     }
227
228
229
230     void kill_channel(Sleeper slp){
231
232         synchronized(channels){
233
234             for (int i = 0;i < top;i++){
235
236                 if (channels[i] != null){
237
238           if (channels[i].slp == slp){
239
240               destroy_channel(i);
241
242               return;
243
244           }
245
246                 }
247
248             }
249
250         }
251
252     }
253
254
255
256     private void count(){
257
258         int sz = top;
259
260         for (int i = 0;i < sz;i++){
261
262             if (channels[i] != null){
263
264                 if (channels[i].time != 0){
265
266                     channels[i].time--;
267
268                     if (channels[i].time == 0){
269
270                         channels[i].slp.wakeup();
271
272                     }
273
274                 }
275
276             }
277
278         }
279
280     }
281
282
283
284     public void run(){
285
286         try {
287
288             for (;;){
289
290                 if (stoped){
291
292                     suspend();
293
294                 } else {
295
296                     sleep(1000);
297
298                     count();
299
300                 }
301
302             }
303
304         } catch(Throwable JavaDoc e){
305
306             e.printStackTrace();
307
308             System.exit(1);
309
310         }
311
312     }
313
314
315
316 }
317
318
319
320
321
322
323
324
325
326
327
328
329
330
Popular Tags