BowlerKernel
PausableTime.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.pid;
2 
3 import java.util.ArrayList;
4 
5 public class PausableTime {
6  private static long timePaused = 0;
7  private static long durationPaused = 0;
8  private static boolean paused =false;
9  private static ArrayList<IPauseTimeListener> listeners = new ArrayList<IPauseTimeListener>();
10 
11  public static long currentTimeMillis() {
12  if(!paused)
13  return System.currentTimeMillis()-durationPaused;
14  return timePaused;
15  }
16 
17  public static void pause(boolean val) {
18  if(val)
19  timePaused=System.currentTimeMillis();
20  else
21  durationPaused+=(System.currentTimeMillis()-timePaused);
22 
23  paused=val;
24  for(int i=0;i<listeners.size();i++)
25  listeners.get(i).pause(val);
26  }
27 
28 
29  public static void step(long ms) {
30  new Thread(()->{
31  boolean start = paused;
32  pause(false);
33  sleep(ms);
34  pause(start);
35  }).start();
36  }
37 
38  public static void sleep(long durationMS) {
39  try {
40  Thread.sleep(durationMS);
41  } catch (InterruptedException e) {
42  throw new RuntimeException(e);
43  }
44  while(paused) {
45  try {
46  Thread.sleep(1);
47  } catch (InterruptedException e) {
48  throw new RuntimeException(e);
49  }
50  }
51  }
52 
53  public static void addIPauseTimeListener(IPauseTimeListener l) {
54  if(listeners.contains(l))
55  return;
56  listeners.add(l);
57  }
59  if(listeners.contains(l))
60  listeners.remove(l);
61  }
62 }
static ArrayList< IPauseTimeListener > listeners
static void addIPauseTimeListener(IPauseTimeListener l)
static void removeIPauseTimeListener(IPauseTimeListener l)
static void sleep(long durationMS)