I hate using Thread.sleep and its almost always a sign of some sort of area where a better design can be used. However in this case I cant seem to think of one. I see a lot of other questions and there is always someone who says 'why are you doing this' , 'what are you doing' or 'you're doing it wrong' :-)
Here, I'm implementing a 'pulse guiding' feature in an astronomical telescope control system. Pulse guide operations 'nudge' the mount a little bit north,south,east or west for a specified time-span. Generally another application will decide how much to 'nudge' it measured in milliseconds. This other application also requires the guide operation to be a blocking operation so they know when to start taking image measurements for the next guide pulse.
So my code look something like this. How can I avoid a Thread.sleep here?
/**
* Pulse guide, move the mount at GUIDE_RATE_MOVE
* for the number of milliseconds passed
*
* @param ms number of milliseconds to move the mount
* @param axis the Axis (RA or DEC)
* @direction true for positive direction false for negative
*/
public void guide(int ms, Axis axis, boolean positiveDirection) throws InterruptedException {
serialAdapter.sendSerialCommand(new Move(mount, GUIDE_RATE_MOVE, axis, direction));
Thread.sleep(ms);
serialAdapter.sendSerialCommand(new Move(mount, GUIDE_RATE_STOP, axis, direction));
}