Java Reference
In-Depth Information
// Don't let data production get more than
// 8 values ahead of the DataGetter
private static final int MAXGAP = 8;
private String [] fBuffer;
private int fBufIndex = 0; // sensor data buffer index
private int fGetIndex = 0; // data reading index
private final long fStart = System.currentTimeMillis ();
boolean fFlag = true;
Outputable fOutput;
/** Constructor creates buffer. Gets Outputable ref. **/
Sensor (Outputable out) {
fOutput = out;
fBuffer = new String [BUFFER - SIZE];
}
/** Turn off sensor readings. **/
public void stopData () {
fFlag = false;
}
/** Take sensor readings in a loop until flag set false.
**/
public void run () {
// Measure the parameter of interest
while (fFlag) sense ();
}
/** Use clock readings to simulate data. **/
private final String simulateData () {
return "" + (int) (System.currentTimeMillis () —
start);
}
/** Use indices fBufIndex, fGetIndex, and the lag()
* method to implement a first-in-first-out (FIFO)
* buffer. **/
synchronized void sense () {
// Don't add more to the data buffer until the getIndex
// has reached within the allow range of bufIndex.
while (lag () > MAXGAP) {
try { wait (); }
catch (Exception e) {}
}
fBuffer[fBufIndex] = simulateData ();
fOutput.println( " Sensor[ " + (fBufIndex) + " ] ="
+ fBuffer[fBufIndex]);
 
Search WWH ::




Custom Search