1 22 package info.monitorenter.gui.chart.io; 23 24 import info.monitorenter.gui.chart.ITrace2D; 25 import info.monitorenter.gui.chart.TracePoint2D; 26 27 28 58 public abstract class ADataCollector implements Runnable { 59 60 61 private boolean m_isRunning = false; 62 63 64 private long m_latency = 400; 65 66 67 private boolean m_stop = true; 68 69 75 private Thread m_thread; 76 77 78 private ITrace2D m_trace; 79 80 90 public ADataCollector(final ITrace2D trace, final long latency) { 91 super(); 92 this.m_latency = latency; 93 this.m_trace = trace; 94 } 95 96 109 public abstract TracePoint2D collectData(); 110 111 114 public void finalize() throws Throwable { 115 super.finalize(); 116 this.stop(); 117 } 118 119 125 public long getLatency() { 126 return this.m_latency; 127 } 128 129 135 public ITrace2D getTrace() { 136 return this.m_trace; 137 } 138 139 145 public boolean isRunning() { 146 return this.m_isRunning; 147 } 148 149 152 public void run() { 153 if (Thread.currentThread() != this.m_thread) { 154 throw new IllegalStateException ( 155 "You cannot start an own thread for data collectors. Use collector.start()!"); 156 } 157 this.m_isRunning = true; 158 long lasttime; 159 this.m_stop = false; 160 TracePoint2D point; 161 while (!this.m_stop) { 162 lasttime = System.currentTimeMillis(); 163 point = this.collectData(); 164 this.m_trace.addPoint(point); 165 try { 166 Thread.sleep(Math.max(this.m_latency - System.currentTimeMillis() + lasttime, 0)); 167 } catch (InterruptedException e) { 168 this.stop(); 169 } 170 if (Thread.interrupted()) { 171 this.stop(); 172 } 173 } 174 this.m_isRunning = false; 175 } 176 177 184 public void setLatency(final long latency) { 185 this.m_latency = latency; 186 } 187 188 211 public void start() { 212 if (this.m_stop) { 213 this.m_thread = new Thread (this); 214 this.m_thread.start(); 215 } 216 } 217 218 234 public void stop() { 235 this.m_stop = true; 236 } 237 238 } 239 | Popular Tags |