Quote:
Originally Posted by lopsided98
This method would work in this case (assuming the Solenoid methods are thread-safe), but I would be worried about possible race conditions if more complicated logic were to later be added in the TimerTask.
Also: Hi Sam!
|
Hey Ben!
Solenoid methods are thread-safe. I'd be more worried about scheduling a close, doing some stuff, then opening the solenoid (
and expecting it to stay open) before the scheduled close happens. In this case, the methods for opening/closing the solenoid should have checks to see if it's been claimed by a different call.
Code:
private boolean claimed = false;
public void openSolenoid() {
if (!claimed) {
claimed = true;
// open it
}
}
public void closeSolenoid() {
if (claimed) {
claimed = false;
// close it
}
}
But I don't think it's a problem for something this simple.