0

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));
}  
Derek
  • 760
  • 2
  • 11
  • 20
  • Consider going to codereview.stackexchange.com for working code. – GhostCat Jan 14 '17 at 12:48
  • 2
    Possible duplicate of [Java - alternative to thread.sleep](http://stackoverflow.com/questions/9143719/java-alternative-to-thread-sleep) – try-catch-finally Jan 14 '17 at 13:00
  • I would use a ScheduledExecutorService and schedule tasks. The advantage being you can cancel them at any time. It's a lot harder to cancel a sleep properly – Guillaume F. Jan 14 '17 at 14:53

0 Answers0