BowlerKernel
ThreadedNsTimer.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.util;
2 
3 // TODO: Auto-generated Javadoc
7 public class ThreadedNsTimer extends Thread {
8 
11 
13  private long startTime;
14 
16  private long currentInterval;
17 
19  private long timerInterval;
20 
22  private long loopIndex=0;
23 
25  private boolean running=true;
26 
28  private boolean failOnRealtime;
29 
37  public ThreadedNsTimer(IThreadedNsTimerListener l, long nsInterval, boolean failOnRealtime) {
38  listener=l;
39  timerInterval = nsInterval;
40  this.failOnRealtime = failOnRealtime;
41  if(l==null)
42  throw new NullPointerException();
43  if(nsInterval<10000){
44  throw new RuntimeException("This is below the resolution of the timer: "+nsInterval);
45  }
46 
47  }
48 
54  private long recalculateTarget(){
56 
57  return System.nanoTime()-currentInterval;
58  }
59 
60  /* (non-Javadoc)
61  * @see java.lang.Thread#run()
62  */
63  public void run(){
64  setName("Bowler Platform Threaded timer instance");
65  setStartTime(System.nanoTime());
67  while(isRunning()){
68  java.util.concurrent.locks.LockSupport.parkNanos(1);
69  long diff = recalculateTarget();
70  if(diff>0){
72  if(recalculateTarget()>0){
73  if(failOnRealtime ){
74  try{
75  throw new RuntimeException("Real time broken!!");
76  }catch(Exception ex){
77  ex.printStackTrace();
78  }
79  }
80  }
81  }
82  }
83  throw new RuntimeException("Real time exeted");
84  }
85 
91  public boolean isRunning() {
92  return running;
93  }
94 
100  public void setRunning(boolean running) {
101  this.running = running;
102  }
103 
109  public long getStartTime() {
110  return startTime;
111  }
112 
118  private void setStartTime(long startTime) {
119  this.startTime = startTime;
120  }
121 
122 
123 }
ThreadedNsTimer(IThreadedNsTimerListener l, long nsInterval, boolean failOnRealtime)