Java Reference
In-Depth Information
// Increment index to next slot for new data
fBufIndex++;
// Circle back to bottom of array if reaches top
if (fBufIndex == BUFFER - SIZE) fBufIndex = 0;
notifyAll ();
} // sense
/** Calculate distance the DataGetter is running behind
* the production of data. **/
int lag () {
int dif = fBufIndex — fGetIndex;
if (dif < 0) dif += BUFFER - SIZE;
return dif;
}
/** Get a data reading from the buffer. **/
synchronized String get () {
// When indices are equal, wait for new data.
while (fBufIndex == fGetIndex) {
try { wait(); }
catch (Exception e) {}
}
notifyAll ();
// Get data at current index
String data = fBuffer[fGetIndex];
// Increment pointer of next datum to get.
fGetIndex++;
// Circle back to bottom of array if reaches top
if (fGetIndex == BUFFER - SIZE) fGetIndex = 0;
return data;
} // get
} // class Sensor
The DateGetter grabs a data value from the sensor after random delay until it
gets its maximum number of data values. Figure 8.7 shows typical output from
DataSync .
import java.util.*;
/** This class obtains sensor data via the get () method.
*Tosimulate random accesses to the sensor, it will
* sleep for brief periods of different lengths after
* every access. After the data is obtained, this thread
Search WWH ::




Custom Search