Java Reference
In-Depth Information
public class
DataSyncApplet
extends JApplet
implements Outputable, ActionListener
{
. ..Build the interface...
/** Create Sensor and DataGetter thread instances and
* start them filling and getting from a Box instance.
**/
public void start()
{
// Create the Sensor and start it
Sensor s
=
new Sensor (this);
s.start ();
// Create DataGetter and tell it to obtain
// 100 sensor readings.
DataGetter dg = new DataGetter (s, 100, this);
dg.start ();
} // start
...
} // class DataSyncApplet
The
Sensor
(see code below) produces one data value (just a string containing
the number of milliseconds since the program began) and stores it in an ele-
ment of a buffer array. The
fBufIndex
keeps track of where the next value
should go. When it reaches the end of the array, it will circle back to the start.
The
fGetIndex
marks the value in the buffer that will be sent next to the
DataGetter
. The
fGetIndex
should never fall farther behind
fBufIndex
than the
MAXGAP
value (set here to 8). If the lag reaches the value of
fMaxGap
then the sensor goes into a loop with an invocation of
wait()
for each pass.
When the
DataGetter
invokes the
get()
method, the
notifyAll()
will
wake the
Sensor
thread from its wait state and it will check the lag again. If it is
no longer at the maximum, the process leaves the wait loop and produces more
data. Otherwise, it loops back around and invokes
wait()
again.
import java.util.*;
/**
* This class represents a sensor producing data
* that the DataGetter objects want to read.
*/
public class
Sensor
extends Thread
{
// Size of the data buffer.
private static final int BUFFER
-
SIZE = 10;
Search WWH ::
Custom Search