BowlerKernel
ThreadedTimeout.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.common;
16 import java.util.ArrayList;
17 
18 import com.neuronrobotics.sdk.util.ThreadUtil;
19 
20 
21 // TODO: Auto-generated Javadoc
25 public class ThreadedTimeout {
26 
28  private static timerThreadClass timerThread;
29  static{
30  timerThread = new timerThreadClass();
31  timerThread.start();
32  }
33 
35  private long time;
36 
37 
40 
42  private long startTime=0;
43 
44 
48  public ThreadedTimeout() {
49 
50  }
51 
57  public boolean isTimedOut() {
58  return System.currentTimeMillis()>(getStartTime()+getAmountOfTimeForTimerToRun());
59  }
60 
67  public void initialize(long sleepTime,IthreadedTimoutListener listener) {
68  setStartTime(System.currentTimeMillis());
69  this.time = (sleepTime);
71  timerThread.addTimer(this);
72  }
73 
80  return time;
81  }
82 
89  this.listener = listener;
90  }
91 
92 
96  private static class timerThreadClass extends Thread{
97 
99  private ArrayList<ThreadedTimeout> timers = new ArrayList<ThreadedTimeout>();
100 
102  private ArrayList<ThreadedTimeout> toRemove = new ArrayList<ThreadedTimeout>();
103  /* (non-Javadoc)
104  * @see java.lang.Thread#run()
105  */
106  public void run(){
107  setName("Bowler Platform Threaded timeout");
108  while(true){
109  if(timers.size()>0){
110  toRemove.clear();
111 
112  for(int i=0;i<timers.size();i++){
113  try{
114  ThreadedTimeout t = timers.get(i);
115  if(t!=null){
116  if(t.isTimedOut()){
117  if(t.listener!=null){
118  t.listener.onTimeout("Timer timed out after "+t.time);
119  }
120  toRemove.add(t);
121  }
122  }
123  }catch (IndexOutOfBoundsException e){
124  //ignore edge case, try again later
125  }catch (Exception ex){
126  ex.printStackTrace();
127  }
128  }
129  for(int i=0;i<toRemove.size();i++){
130  removeTimer(toRemove.get(i));
131  }
132  }
133  ThreadUtil.wait(1);
134  }
135  }
136 
142  public void addTimer(ThreadedTimeout time){
143  try{
144  synchronized(timers){
145  if(!timers.contains(time))
146  timers.add(time);
147  }
148  }catch (Exception e){
149  e.printStackTrace();
150  }
151  }
152 
158  public void removeTimer(ThreadedTimeout time){
159  try{
160  synchronized(timers){
161  if(timers.contains(time))
162  timers.remove(time);
163  }
164  }catch (Exception e){
165  e.printStackTrace();
166  }
167 
168  }
169  }
170 
171 
175  public void stop() {
176 
177  timerThread.removeTimer(this);
178  }
179 
185  public long getStartTime() {
186  return startTime;
187  }
188 
194  public void setStartTime(long startTime) {
195  this.startTime = startTime;
196  }
197 }
void setTimeoutListener(IthreadedTimoutListener listener)
void initialize(long sleepTime, IthreadedTimoutListener listener)