KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > util > TimeoutThread


1 package org.jacorb.trading.util;
2 import java.util.*;
3
4 /**
5  * TimeoutThread.java
6  *
7  *
8  * Created: Sat Feb 5 11:45:40 2000
9  *
10  * @author Nicolas Noffke
11  * @version $Id: TimeoutThread.java,v 1.6 2004/04/28 12:37:29 brose Exp $
12  */

13
14 public class TimeoutThread extends Thread JavaDoc {
15   private int timeout = 0;
16   private TimerListNode last = null;
17   private TimerListNode first = null;
18   private Hashtable current_nodes = null;
19
20   public TimeoutThread(int timeout){
21     this.timeout = timeout;
22     last = new TimerListNode();
23     first = last;
24     current_nodes = new Hashtable();
25
26     start();
27   }
28
29   public void run(){
30     while(true){
31       try{
32     //blocks, until node is available
33
first = first.getNext();
34         
35     if (first.wakeup_time <= System.currentTimeMillis())
36       first.doInterrupt();
37     else{
38       sleep(Math.abs(first.wakeup_time -
39              System.currentTimeMillis()));
40       first.doInterrupt();
41     }
42
43       }catch (Exception JavaDoc e){
44       }
45     }
46   }
47
48   /**
49    * Stop the alarm timer.
50    */

51   public void stopTimer(Thread JavaDoc interruptee){
52
53     TimerListNode _current = (TimerListNode) current_nodes.get(interruptee);
54     _current.stopTimer();
55   }
56     
57   /**
58    * This method sets a timeout, after wich an interrupt() is scheduled.
59    *
60    * @param interruptee the thread to interrupt.
61    */

62   public synchronized void setTimeout (Thread JavaDoc interruptee){
63     //create new node
64
TimerListNode _new = new TimerListNode(interruptee, timeout +
65                        System.currentTimeMillis());
66     //hook into list
67
last.setNext(_new);
68     last = _new;
69     current_nodes.put(interruptee, _new);
70   }
71 } // TimeoutThread
72

73
74
75
76
77
78
79
80
81
82
83
84
Popular Tags