KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > core > Timer


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.core;
25
26 public class Timer extends Thread JavaDoc
27 {
28     private int m_timeout;
29     private int m_elapsed;
30     private boolean m_exitFlag;
31     private boolean m_hasExpired;
32     private java.io.ObjectInputStream JavaDoc m_input;
33
34     public Timer(java.io.ObjectInputStream JavaDoc input, int timeout)
35     {
36         m_elapsed = 0;
37         m_exitFlag = false;
38         m_hasExpired = false;
39
40         m_input = input;
41         m_timeout = timeout;
42
43         setDaemon(true);
44     }
45
46     private synchronized void expired()
47     {
48         m_hasExpired = true;
49
50         halt();
51
52         try
53         {
54             m_input.close();
55         }
56         catch (Exception JavaDoc e)
57         {
58             // nothing to do
59
}
60     }
61
62     public boolean hasExpired()
63     {
64         return m_hasExpired;
65     }
66
67     public synchronized void halt()
68     {
69         m_exitFlag = true;
70     }
71
72     public synchronized void restart()
73     {
74         m_elapsed = 0;
75     }
76
77     public void run()
78     {
79         java.lang.Thread.currentThread().setName("Timer (" + m_input + ") Timeout Handler");
80
81         while (m_exitFlag == false)
82         {
83             try
84             {
85                 Thread.sleep(100);
86             }
87             catch (Exception JavaDoc e)
88             {
89                 continue;
90             }
91
92             // Use 'synchronized' to prevent conflicts
93
synchronized (this)
94             {
95                 m_elapsed += 100;
96
97                 if (m_elapsed > m_timeout)
98                 {
99                     expired();
100                 }
101             }
102
103         }
104     }
105 }
106
107
Popular Tags