1 package org.jacorb.trading.util; 2 import java.util.*; 3 4 13 14 public class TimeoutThread extends Thread { 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 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 e){ 44 } 45 } 46 } 47 48 51 public void stopTimer(Thread interruptee){ 52 53 TimerListNode _current = (TimerListNode) current_nodes.get(interruptee); 54 _current.stopTimer(); 55 } 56 57 62 public synchronized void setTimeout (Thread interruptee){ 63 TimerListNode _new = new TimerListNode(interruptee, timeout + 65 System.currentTimeMillis()); 66 last.setNext(_new); 68 last = _new; 69 current_nodes.put(interruptee, _new); 70 } 71 } 73 74 75 76 77 78 79 80 81 82 83 84 | Popular Tags |