Java Reference
In-Depth Information
else
for
(
int
i
#
0; i < sensors.size(); i
!!
) {
Device sensor
#
(Device) sensors.get(i);
if
(deviceName.equals(sensor.name))
return
sensor.println(command);
}
return false
;
}
}
Class
Device
implements the concurrency mechanisms that handle the
dynamics of the robot's components. It encapsulates the list of commands
that the device receives from the controller and implements the
run()
method of the
Runnable
interface to process these commands.
public abstract class
Device
implements
Runnable{
protected
ArrayList commands
#
new
ArrayList();
// the list of commands
protected
Thread thread;
// the reference to a Thread object
protected
long delay
#
20;
// the delay between two steps in millisecs
protected
boolean alive
#
false
;
// true when the thread is started
protected
boolean running
#
false
;
// true when it is executing a command
protected
Object lock
#
new
Object();
// a lock for thread synchronization
...
public
Device(String name, Position global,
Position local, Environment env) {
...
thread
#
new
Thread(
this
);
}
// Inserts a new command in the command list and notifies
// the thread
public boolean
println(String message) {
commands.add(command);
// inserts the command in the commands' list
synchronized
(lock) { lock.notify();}
// notifies the thread
return true
;
}
protected synchronized void
writeOut(String data) {
System.out.println(data);
}
// The start() method activates the thread that executes
// this device