01: ///////////////////////////////////////////////////////////////////////////////
02: //
03: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
04: //
05: // All Rights Reserved
06: //
07: // This program is free software; you can redistribute it and/or modify
08: // it under the terms of the GNU General Public License and GNU Library
09: // 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 {
27: private int m_timeout;
28: private int m_elapsed;
29: private boolean m_exitFlag;
30: private boolean m_hasExpired;
31: private java.io.ObjectInputStream m_input;
32:
33: public Timer(java.io.ObjectInputStream input, int timeout) {
34: m_elapsed = 0;
35: m_exitFlag = false;
36: m_hasExpired = false;
37:
38: m_input = input;
39: m_timeout = timeout;
40: }
41:
42: private synchronized void expired() {
43: m_hasExpired = true;
44:
45: halt();
46:
47: try {
48: m_input.close();
49: } catch (Exception e) {
50: // nothing to do
51: }
52: }
53:
54: public boolean hasExpired() {
55: return m_hasExpired;
56: }
57:
58: public synchronized void halt() {
59: m_exitFlag = true;
60: }
61:
62: public synchronized void restart() {
63: m_elapsed = 0;
64: }
65:
66: public void run() {
67: while (m_exitFlag == false) {
68: try {
69: Thread.sleep(100);
70: } catch (Exception e) {
71: continue;
72: }
73:
74: // Use 'synchronized' to prevent conflicts
75: synchronized (this ) {
76: m_elapsed += 100;
77:
78: if (m_elapsed > m_timeout) {
79: expired();
80: }
81: }
82:
83: }
84: }
85: }
|